Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 405 error for POST method

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:

    @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

And on the client side, I got: Exception in thread "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.

like image 491
ken Avatar asked Oct 22 '13 08:10

ken


People also ask

What is 405 Method not allowed in Postman?

The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the server knows the request method, but the target resource doesn't support this method.


1 Answers

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.

@RequestMapping(value="/course", method = RequestMethod.POST, produces="application/json")
public @ResponseBody List<Course> upload(@RequestBody Course[] cs) {
}
like image 109
ken Avatar answered Nov 15 '22 07:11

ken