【发布时间】: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