I have setup a Interceptor
in my web application. It works fine and I see it getting invoked for all the requests except for one method which accepts POST
requests only. It seems that Spring has mapping for all the controller methods and what request method (like GET
or POST
) is mapped to each controller method. Before Interceptor
is invoked, it sees which request method is mapped to which controller method, and if it doesn't find it, it throws '405 Request method 'GET' not supported` error. So I want to know, how do I tackle that?
To be clear, lets say I have two methods in my controller.
@Controller
public class myController{
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test1(){
return "abc";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String test1(){
return "xyz";
}
and this is my Interceptor
:
public class URLInterceptors extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("REQUESTED SERVLET PATH IS: " + request.getServletPath());
return true;
}
}
And there is no problem in the configuration either:
public class RootContextConfiguration extends WebMvcConfigurerAdapter
{
@Bean
public URLInterceptors urlInterceptors(){
return new URLInterceptors();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(this.urlInterceptors());
}
....
}
Then, whenever a request is made for "/test"
, my Interceptor
is invoked perfectly fine. But whenever a request is made to "/login"
, my Interceptor
is never invoked. Instead I see a 405 Request method 'GET' not supported
error.
The error:
405 Request method 'GET' not supported
Means whatever client side request you are making against the servlet is a GET request. The problem isn't with the servlet. I don't know if you are using a REST client or a browser etc, but you need to check and make sure the request that is being sent to your servlet is a POST