POST 方法的 Spring 405 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19512564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 405 error for POST method
提问by ken
I'm trying to secure my website using Spring security following the guides on the web. I don't want my users to use my application through web browsers, so I disabled the csrf protection. The source code on the server side:
我正在尝试按照网络上的指南使用 Spring 安全保护我的网站。我不希望我的用户通过网络浏览器使用我的应用程序,所以我禁用了 csrf 保护。服务器端的源代码:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
implements ApplicationContextAware {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().authenticated().and()
.httpBasic().and()
.csrf().disable();
}
@Override
protected void registerAuthentication(AuthenticationManagerBuilde r authManagerBuilder) throws Exception {
authManagerBuilder.inMemoryAuthentication()
.withUser("user").password("password").roles("ADMI N");
}
}
@Controller
//@RequestMapping("/course")
public class CourseController implements ApplicationContextAware{
@RequestMapping(value="/course", method = RequestMethod.GET, produces="application/json")
public @ResponseBody List<Course> get(// The critirion used to find.
@RequestParam(value="what", required=true) String what,
@RequestParam(value="value", required=true) String value) {
//.....
}
@RequestMapping(value="/course", method = RequestMethod.POST, produces="application/json")
public List<Course> upload(@RequestBody Course[] cs) {
}
}
I am using RestTemplate on the client side. The problem is that when I use POST method, I got warinning on the server side: o.s.web.servlet.PageNotFound : Request method 'POST' not supported
我在客户端使用 RestTemplate。问题是,当我使用 POST 方法时,我在服务器端收到警告: osweb.servlet.PageNotFound : Request method 'POST' not supported
And on the client side, I got: Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed
在客户端,我得到了:线程“main”org.springframework.web.client.HttpClientErrorException:405 Method Not Allowed 中的异常
It is odd I got this exception because I already have the POST method handled in the Controller. Currently, the system still works, but this issue bothers me. Any idea? Thanks.
我收到这个异常很奇怪,因为我已经在控制器中处理了 POST 方法。目前,该系统仍然有效,但这个问题困扰着我。任何的想法?谢谢。
回答by ken
Finally, I found what was wrong. Hope this helpful for someone else. This mistake is quite stupid. The return value of upload() should use @ResponseBody, because I want to return the courses directly.
终于,我发现了哪里不对劲。希望这对其他人有帮助。这个错误非常愚蠢。upload() 的返回值应该使用@ResponseBody,因为我想直接返回课程。
@RequestMapping(value="/course", method = RequestMethod.POST, produces="application/json")
public @ResponseBody List<Course> upload(@RequestBody Course[] cs) {
}
回答by Ashwani
Not sure but try with setting both methods like this:
不确定,但尝试像这样设置两种方法:
@RequestMapping(value="/course", method = { RequestMethod.GET, RequestMethod.POST } produces="application/json")
public @ResponseBody List<Course> get(// The critirion used to find.
@RequestParam(value="what", required=true) String what,
@RequestParam(value="value", required=true) String value) {
//.....
}
@RequestMapping(value="/course", method = { RequestMethod.GET, RequestMethod.POST } , produces="application/json")
public List<Course> upload(@RequestBody Course[] cs) {
}

