更改servlet映射会引发" HTTP状态405 - 请求方法' GET'不支持"

时间:2015-08-18 07:58:47

标签: java spring-mvc servlets

我遇到了一个问题:最初我有这个servlet-mapping:

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

并且所有控制器映射到example.html,example2.html网址都很好。但在某些情况下,我想使用* .json映射,在这种情况下,我通过这种方式更改了servlet映射:

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

在这次改变之后我得到了

  

HTTP状态405 - 请求方法&#39; GET&#39;不支持

我的控制器抛出错误的示例:

       @RequestMapping(value="example.html",method = RequestMethod.GET)
      public String example(
          @RequestParam(value = "q", defaultValue = "") String query,
          @RequestParam(value = "page", required = false, defaultValue = "1") int page,
          HttpServletRequest request, Model model)  {

        String template = "printout-blog";
    model.addAttribute("q", query);
    return template;
}

1 个答案:

答案 0 :(得分:2)

您将url-pattern定义为/*,这意味着调度程序将处理所有传入的请求。将您的请求映射更改为下一行中的代码段! (url应该以斜杠/开头),一切都会正常工作

 @RequestMapping(value="/example.html",method = RequestMethod.GET)
      public String example(
          @RequestParam(value = "q", defaultValue = "") String query,
          @RequestParam(value = "page", required = false, defaultValue = "1") int page,
          HttpServletRequest request, Model model){}