Spring 405 POST方法的错误

Spring 405 POST方法的错误,spring,spring-security,Spring,Spring Security,我正试图按照网上的指南使用Spring security来保护我的网站。我不希望我的用户通过web浏览器使用我的应用程序,所以我禁用了csrf保护。服务器端的源代码: @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter implements ApplicationContextAware { @Override protecte

我正试图按照网上的指南使用Spring security来保护我的网站。我不希望我的用户通过web浏览器使用我的应用程序,所以我禁用了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) {
}
}
@配置
@启用Web安全性
公共类WebSecurityConfig扩展了WebSecurityConfigureAdapter
实现ApplicationContextAware{
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http
.authorizeRequests().anyRequest().authorized()和()
.httpBasic()和()
.csrf().disable();
}
@凌驾
受保护的无效注册表身份验证(AuthenticationManagerBuilder r authManagerBuilder)引发异常{
authManagerBuilder.inMemoryAuthentication()
.withUser(“用户”)。密码(“密码”)。角色(“管理员”);
}
}
@控制器
//@请求映射(“/course”)
公共类CourseController实现ApplicationContextAware{
@RequestMapping(value=“/course”,method=RequestMethod.GET,products=“application/json”)
public@ResponseBody List get(//用于查找的临界值。
@RequestParam(value=“what”,required=true)字符串what,
@RequestParam(value=“value”,required=true)字符串值){
//.....
}
@RequestMapping(value=“/course”,method=RequestMethod.POST,products=“application/json”)
公共列表上载(@RequestBody课程[]cs){
}
}
我在客户端使用RestTemplate。问题是,当我使用POST方法时,我在服务器端得到了警告: o、 s.web.servlet.PageNotFound:不支持请求方法“POST”

在客户方面,我得到: 线程“main”org.springframework.web.client.HttpClientErrorException中出现异常:不允许使用405方法

奇怪的是,我得到了这个异常,因为我已经在控制器中处理了POST方法。目前,该系统仍然工作,但这个问题困扰着我。 有什么想法吗?
谢谢。

不确定,但请尝试如下设置这两种方法:

@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) {
}
@RequestMapping(value=“/course”,method={RequestMethod.GET,RequestMethod.POST}products=“application/json”)
public@ResponseBody List get(//用于查找的临界值。
@RequestParam(value=“what”,required=true)字符串what,
@RequestParam(value=“value”,required=true)字符串值){
//.....
}
@RequestMapping(value=“/course”,method={RequestMethod.GET,RequestMethod.POST},products=“application/json”)
公共列表上载(@RequestBody课程[]cs){
}

最后,我发现了问题所在。希望这对其他人有帮助。 这个错误很愚蠢。upload()的返回值应该使用@ResponseBody,因为我想直接返回课程

@RequestMapping(value="/course", method = RequestMethod.POST, produces="application/json")
public @ResponseBody List<Course> upload(@RequestBody Course[] cs) {
}
@RequestMapping(value=“/course”,method=RequestMethod.POST,products=“application/json”)
public@ResponseBody列表上载(@RequestBody课程[]cs){
}

如果它不起作用,则可能需要扩展类
AnnotationMethodHandlerAdapter
不起作用。它给出了一个模糊的控制器错误。因此,我想现在必须尝试自定义AnnotationMethodHandlerAdapter对于我来说,当我调用正确类型的方法(PUT而不是POST)时,错误得到了解决。可能是您的上载方法注释不足吗?get方法上有一个@ResponseBy注释,但您没有。请在此处发布完整的upload()方法。对我来说,在调用正确类型的方法(PUT而不是post)时,错误得到了解决