【问题标题】:Spring Boot POST request doesn't workSpring Boot POST 请求不起作用
【发布时间】:2017-08-10 21:02:20
【问题描述】:

我正在尝试使用 angularJS 构建 Spring Boot REST 应用程序。

所以,应用程序加载良好,包括每个 JS 和 CSS 文件。问题是,当我做 GET 请求时,它是正确的,但是当我做 POST 请求时,它失败并且不会尝试调用控制器方法。

这是我的 Spring Boot 应用程序类

@EnableAspectJAutoProxy(proxyTargetClass=true)
@SpringBootApplication(scanBasePackages = {"org.test.controllers", "org.test.services"})
@Import({ WebSecurityConfig.class, DBConfig.class, ViewConfig.class})
public class Application extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        System.out.println("STARTING APP");
        SpringApplication.run(Application.class, args);
    }

}

这就是我的控制器类

@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    private TagService tagService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public Iterable<Tag> getAllTags() {
        return tagService.getAll();
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public Tag saveTag(@RequestBody Tag tag) {
        return tagService.save(tag);
    }
}

所以,当我执行$http.get("/tag", success, error) 时,它会给出[],这意味着调用了控制器。

当我在做$http.post("/tag", {name: 'name'}, success, error) 时,它返回{"timestamp":1489939480282,"status":404,"error":"Not Found","message":"No message available","path":"/tag"}

为确保已完成映射,这是日志的一部分

Mapped "{[/tag],methods=[POST]}" onto public org.test.model.Tag org.expotest.controllers.TagController.saveTag(org.test.model.Tag)

如果重要的话,我会在 Tomcat 服务器上运行。

任何想法我的配置可能有什么问题?这对我来说真的很奇怪。

提前致谢。

【问题讨论】:

  • 您是否尝试通过某个 REST 客户端进行 POST?
  • 能否请您发布您的模型标签类.....您是否使用邮递员测试了相同的有效负载?

标签: java angularjs spring spring-mvc spring-boot


【解决方案1】:

如果您使用 Spring 安全性,请尝试在您的配置方法中禁用 csrf,应该如下所示:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
      .csrf().disable();
}

【讨论】:

    猜你喜欢
    • 2015-09-30
    • 1970-01-01
    • 2018-08-06
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多