Enable HTTP POST Request in Spring Download

I am using spring boot, here is the maven dependency

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 

For web pages, I place the files in the src / main / resources / static file. There I have html files, js libraries (angular, jquery) and css files.

I am trying to make an HTTP POST request using Angular (I also have a GET request that works fine), but I get this

 POST http://localhost:8080/xxxx/12/addEntry 405 (Method Not Allowed) 

In response headers

 HTTP/1.1 405 Method Not Allowed Server: Apache-Coyote/1.1 X-Application-Context: application Allow: HEAD, GET Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Wed, 09 Jul 2014 13:04:05 GMT 

I understand that allow does not have a POST method in the answer.

Method in the controller

 @RequestMapping(value = "/xxxx/{uid}/addEntry", method = RequestMethod.POST) @ResponseBody public String createEntry(@PathVariable String uid, @RequestBody String form) { System.out.println(form); return "index.html"; } 
+7
spring spring-boot spring-mvc
source share
3 answers

Sometimes, especially during the initial testing of Spring csrf, Cross Site Request Forgery is the default protection and prevents POST requests from being executed, a temporary temporary one is to disable csrf. This is usually done in your Web Security Config class, which extends WebSecurityConfigurerAdapter

 @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable(); } } 

Note. This works like on Spring boot version 2.0.0.RC1 and better if this one is NOT used as a constant work around

+1
source

Another solution worked for me. I just needed to add the correct annotation to the controller itself, for example:

 @RestController public class EntriesController { //your code here } 
+2
source

It was a long time ago, sorry for not sending an answer at that time, but I will try to explain what, in my opinion, happened.

I tried to check the ajax request with the Chrome Postman plugin, but it was not possible because I had a CORS problem (I could not do ajax using the POST method because the server allowed me to do HEAD or GET).

On the same server, I had an angular application (this is the application that was supposed to execute the POST request) and the Java API (this is the application that was expecting the POST request), so, t is a CORS problem. But that did not work, because I made another mistake, which was that in the post method of the angular application I did not send the payload data to POST.

 @RequestMapping(value = "/xxxx/{uid}/addEntry", method = RequestMethod.POST) @ResponseBody public String createEntry(@PathVariable String uid, @RequestBody String form) { System.out.println(form); return "index.html"; } 

I hope this makes the answer clearer. Thanks for reading.

0
source

All Articles