在Spring Boot中启用HTTP请求POST

agu*_*ina 9 spring spring-mvc spring-boot

我正在使用Spring启动,这里是maven依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

对于网页,我将文件放在src/main/resources/static中.我有我的html文件,js库(angular,jquery)和css文件.

我正在尝试使用Angular进行HTTP请求POST(我也有一个正常工作的GET请求),但我得到了这个

POST http://localhost:8080/xxxx/12/addEntry 405 (Method Not Allowed) 
Run Code Online (Sandbox Code Playgroud)

在响应标题中

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
Run Code Online (Sandbox Code Playgroud)

我意识到在响应中,allow没有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";
}
Run Code Online (Sandbox Code Playgroud)

Dee*_*pak 5

有时,特别是在初始测试期间,Spring 的csrf - 跨站点请求伪造 - 保护默认启动并阻止 POST 请求发生,临时解决方法是禁用csrf。这通常在扩展 WebSecurityConfigurerAdapter 的 Web Security Config 类中完成

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable();
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:这在Spring boot 版本 2.0.0.RC1上有效,如果将其用作永久解决方案,则最好