0

I have a problem: initially I had this servlet-mapping:

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

and all was pretty fine with controllers mapped to example.html, example2.html urls. But in some cases I want to use *.json mapping and for this case I changed servlet mapping by this way:

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

After this change I got

HTTP Status 405 - Request method 'GET' not supported

Example of my controller which throws the error:

       @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;
}
Filosssof
  • 1,288
  • 3
  • 17
  • 37
  • 1
    Why don't you just add another URL pattern `*.json` to the existing mapping? Using `/*` on servlets is a huge smell. See also a.o. http://stackoverflow.com/a/4140659 – BalusC Aug 18 '15 at 08:25

1 Answers1

2

you defined your url-pattern to be /* which means the dispatcher will handle all incoming requests. Change your request mapping to be like code snippet in the next lines! ( url should start with slash /) and everything will work fine

 @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){}
Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29