In my application, I have several RequestMappings that only allow POST. If someone accidentally runs a GET request on this particular path, he gets a 405 error page loaded by the container (Tomcat). I can create and define a 405 error page in web.xml for customization.
What I want: any request that results in a 405 error should be handled by a specific controller method.
What I tried:
- a method with "method = GET" as an analog for each of the mappings mentioned. This works fine, but requires that I create an actual requestmapping and method for each path that only POST permits. I find this unnecessary duplication and mess.
- global catch (requestmapping / *) method: this does not work, since Spring accepts the GET method as an invalid call to a path specified using only POST
- ExceptionHandler-annotated method to handle HttpRequestMethodNotSupportedException class exceptions: this does not work. Spring seems to throw and throw this exception completely in its code.
- specify your own 405 in web.xml. This is not ideal as I want to have custom processing, not a static error page.
source share