Rest Controller中的删除方法错误问题 - java

我的项目中有一些Rest端点,这些端点是从另一台服务器的客户端应用程序调用的。我已使用@CrossOrigin批注成功禁用了Cors,除Delete方法在Chrome上引发以下错误外,所有方法均正常运行:
XMLHttpRequest cannot load http://localhost:8856/robotpart/1291542214/compatibilities. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8888' is therefore not allowed access. The response had HTTP status code 403.
这是我的控制器:

@CrossOrigin(origins = "*")
@ExposesResourceFor(RobotPart.class)
public class RobotPartController {

      //All endpoints are working except the Delete Mapping

    @GetMapping("/robotpart")
    public ResponseEntity<List<RobotPartResource>> listAllParts() {
        //..
    }

    @GetMapping("/robotpart/{id}")
    public ResponseEntity<RobotPartResource> getById(@PathVariable Integer id) {
        //..
    }


    @GetMapping("/robotpart/{id}/compatibilities")
    public ResponseEntity<Collection<RobotPartResource>> getRobotCompatibilities(@PathVariable Integer id,
          //..
    }


    @PostMapping("/robotpart")
    public ResponseEntity<RobotPartResource> getById(@RequestBody @Valid RobotPart newRobot) {
        //..

    @PutMapping("/robotpart/{id}")
    public ResponseEntity<RobotPartResource> modify(@PathVariable Integer id, @Valid @RequestBody RobotPart newRobot) {

         //...
    }

    @DeleteMapping("/robotpart/{id}")
    public ResponseEntity<RobotPart> deleteById(@PathVariable Integer id) {

        //...
    }

    }

有什么办法吗?

参考方案

在分析http请求之后,我找到了一个解决方案,我注意到Access-Control-Allow-Methods标头缺少DELETE方法,因此我通过删除@CrossOrigin批注将其添加到配置中并将其添加到配置中:

        @Bean
        public WebMvcConfigurer corsConfigurer() {
            return new WebMvcConfigurerAdapter() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/robotpart/**").allowedOrigins("*").allowedMethods("GET", "POST","PUT", "DELETE");


                }
            };
        }

REST API版本控制 - java

我目前在基于Java的Web应用程序上工作。最近,我们使用Spring创建了一些REST端点。这样做的原因是因为我们开发了一种混合移动应用程序,该应用程序通过这些端点与我们的主应用程序集成。问题是,今后我们还不确定如何处理更新。如果我们更新API,例如我们更改端点方法的方法签名,或者更改作为JSON返回的DTO上的属性,那么如果我们的移动用户运行的是过时版本…

使用哪个Spring和Hibernate版本 - java

Improve this question 我是Spring和Hibernate的新手。我必须开发一些REST API。因此,我需要帮助选择Spring和Hibernate的正确版本。一些初学者建议使用spring 3.0和Hibernat2 2.0。是正确的选择还是我必须使用spring 3.0和spring 3.0? 参考方案 一点都不。这两个版本已完全…

RESTAsured获得两个可能的状态代码之一 - java

我有RESTAssured的test(!)代码,该代码检查REST端点是否将0作为状态代码返回给我; given() .contentType(CONTENT_TYPE_APPLICATION_JSON) .when() .get(getRestOperationPath()) .then() .statusCode(STATUS_CODE_OK); 但是现…

REST URI设计:可选和多值信息传递 - java

我有一个搜索小部件,人们可以通过邮政编码搜索汽车经销商。还有一些可选的复选框可以优化该小部件中的搜索。这是用于按邮政编码搜索经销商的URI。http://localhost:8080/dealer/zip/10080 如果用户选择复选框,则URI为http://localhost:8080/dealer/zip/10080servicetype=type1&…

Spring MVC中的输入验证 - java

我知道Commons Validator框架是Struts项目在服务器端和客户端验证输入值的事实上的标准。Spring MVC项目是否也是如此?我得到的印象可能不是,大多数Struts书籍和论坛都谈论Commons Validator框架,但是只有少数Spring书籍和论坛可以。在Spring MVC项目中验证输入的最佳实践是什么?干杯! 参考方案 在引入S…