【问题标题】:Provide two methods for same rest end-point but differs in PathVariable in Spring为相同的休息端点提供两种方法,但在 Spring 中的 PathVariable 中有所不同
【发布时间】:2018-11-02 10:25:40
【问题描述】:

我有一个要求,我需要根据路径变量将请求正文映射到特定的子类。请求正文本身不包含任何关于选择哪个子类的信息。

@ApiOperation(value = "Update Developer (dev)",
        response = ResponseEntity.class)
@RequestMapping(method = RequestMethod.PATCH,
        value = "/{type}")
public ResponseEntity<Response> updateDeveloper(
        @PathVariable String type,
        @RequestParam("year") String year,
        @RequestBody Developer employeeUpdate,

) { .....}


@ApiOperation(value = "Update Manager (manager)",
        response = ResponseEntity.class)
@RequestMapping(method = RequestMethod.PATCH,
        value = "/{type}")
public ResponseEntity<Response> updateManager(
        @PathVariable String type,
        @RequestParam("year") String year,
        @RequestBody Manager employeeUpdate,

) { .....}

Developer 和 Manager 扩展抽象类 Employee。

我尝试了只有一种方法,如下所示:

@ApiOperation(value = "Update Employee (manager, dev)",
        response = ResponseEntity.class)
@RequestMapping(method = RequestMethod.PATCH,
        value = "/{type}")
public ResponseEntity<Response> updateEmployee(
        @PathVariable String type,
        @RequestParam("year") String year,
        @RequestBody Employee employeeUpdate,

) { .....}

但是 spring 无法将 Employee 实例实例化为其抽象。

我的设计不正确吗?我更喜欢有一个不需要修改 Employee/Developer/Manager 类的解决方案。

提前致谢!!

【问题讨论】:

  • 您误解了“REST 端点”的含义。这不是“会回答的 Java 方法”,而是“路径、方法和生产/消费类型的组合”。你所描述的对我来说看起来像是两个 REST 端点。如果你真的想调用普通的Java方法,那么定义那个方法并从updateDeveloper和updateManager调用它们。就像在 Java 中一样,如果您的接口声明这些更新操作不同,但实现声明它们相同。

标签: java json rest spring-mvc spring-boot


【解决方案1】:

感谢大家对此进行调查。我找到了适合我情况的方法:

@ApiOperation(value = "Update Developer (dev)",
        response = ResponseEntity.class)
@RequestMapping(method = RequestMethod.PATCH,
        value = "/{type:developer}")
public ResponseEntity<Response> updateDeveloper(
        @PathVariable String type,
        @RequestParam("year") String year,
        @RequestBody Developer employeeUpdate,

) { .....}

@ApiOperation(value = "Update Manager (manager)",
        response = ResponseEntity.class)
@RequestMapping(method = RequestMethod.PATCH,
        value = "/{type:manager}")
public ResponseEntity<Response> updateManager(
        @PathVariable String type,
        @RequestParam("year") String year,
        @RequestBody Manager employeeUpdate,

) { .....}

这样,当尝试将请求主体的单一方法和数据类型设置为父对象时,我也不会收到任何实例化错误。

【讨论】:

    【解决方案2】:

    根据 REST 合规性,您应该识别您的资源:

    @RequestMapping(method = RequestMethod.PATCH, value = "/developer")
    
    @RequestMapping(method = RequestMethod.PATCH, value = "/manager")
    

    如果您仍想随机发布正文(开发人员或经理),您是经理和开发人员的组合 DTO:

    public class EmployeeDTO {
        private int type; // Developer or manager
        // All of properties of Manager and Developer
    }
    

    通过检查type,您可以委托正确的服务方法。

    【讨论】:

    • 你已经在路径中包含了类型,所以 {type} 变得不必要了。 (不仅如此,你会得到 404 因为你需要类型,或者总是将类型作为空字符串。不记得具体是哪个)。
    • 404 什么?我认为 OP 希望按类型查找开发人员或按经理类型查找经理等...
    • 是的,但是你已经在路径映射中有“开发者”,那你需要{type}做什么?您在这里有效地声明了路径/developer/&lt;something&gt;。而且我希望您通常不会使用像 /developer/developer 这样的 URI 调用 REST,也不应该有人编写这样的 REST API。
    猜你喜欢
    • 2017-11-13
    • 1970-01-01
    • 2011-05-28
    • 2018-08-05
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2016-03-15
    相关资源
    最近更新 更多