【发布时间】:2017-05-29 08:42:55
【问题描述】:
我在我的网络应用程序中设置了Interceptor。它工作正常,我看到它被所有请求调用,除了一种只接受POST 请求的方法。似乎 Spring 对所有控制器方法都有映射,以及将哪些请求方法(如 GET 或 POST)映射到每个控制器方法。在调用Interceptor 之前,它会查看哪个请求方法映射到哪个控制器方法,如果没有找到,它会抛出'405 Request method 'GET' not supported`错误。所以我想知道,我该如何解决?
为了清楚起见,假设我的控制器中有两种方法。
@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";
}
这是我的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;
}
}
而且配置也没有问题:
public class RootContextConfiguration extends WebMvcConfigurerAdapter
{
@Bean
public URLInterceptors urlInterceptors(){
return new URLInterceptors();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(this.urlInterceptors());
}
....
}
然后,每当对"/test" 发出请求时,我的Interceptor 就会被完美调用。但是每当向"/login" 发出请求时,我的Interceptor 永远不会被调用。相反,我看到了 405 Request method 'GET' not supported 错误。
【问题讨论】:
-
将
RequestMethod.POST更改为GETfor/login? -
@ArsenDavtyan 大声笑没有。有更好的方法来处理这个问题。如果我的登录页面收到表单怎么办?那我肯定需要 POST。
-
问题是如果你得到
405 Request method 'GET' not supported为/login,这意味着你在客户端使用GET,而不是POST
标签: java spring-mvc interceptor http-status-code-405