繁体   English   中英

Spring Boot POST 请求不起作用

[英]Spring Boot POST request doesn't work

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

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

那是我的 Spring Boot Application 类

@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 服务器上运行。

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

提前致谢。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM