简体   繁体   English

具有相同路径但具有不同 @RequestParam 值的多个端点

[英]Multiple endpoints with the same path but with different @RequestParam values

I have an API path GET /users/copy .我有一个 API 路径GET /users/copy I can have two APIs with the same /users/type path but with different sets of RequestParams using the following construction:我可以使用以下构造拥有两个具有相同/users/type路径但具有不同 RequestParams 集的 API:

@GetMapping(value = "/users/copy", params = {"type"})
public ResponseEntity<UserDto> copyUserWithType(@RequestParam UserTypeEnum type) {
    ...
}

@GetMapping(value = "/users/copy", params = {"origin"})
public ResponseEntity<UserDto> copyUserWithOrigin(@RequestParam UserOriginEnum origin) {
    ...
}

BUT in case I need to have different APIs for different user types (like for type = OLD and type = NEW ), is there a way to still have the same GET /users/copy path for them?但是,如果我需要为不同的用户类型使用不同的 API(例如type = OLDtype = NEW ),有没有办法让他们仍然拥有相同的GET /users/copy路径?

Perhaps something like:也许是这样的:

@GetMapping(value = "/users/copy", params = {"type=OLD"})
public ResponseEntity<UserDto> copyUserWithTypeOld(@RequestParam UserTypeEnum type) {
    ...
}  

@GetMapping(value = "/users/copy", params = {"type=NEW"})
public ResponseEntity<UserDto> copyUserWithTypeNew(@RequestParam UserTypeEnum type) {
    ...
}

Actually, the answer was in the question.实际上,答案就在问题中。

In order to have different APIs endpoints with the same path and the same set of @ReuqestParam, but with different @ReuqestParam values you need to specify params attribute as such:为了让不同的 API 端点具有相同的路径和相同的 @ReuqestParam 集,但具有不同的 @ReuqestParam 值,您需要指定params属性,如下所示:

@GetMapping(value = "/users/copy", params = {"type=OLD"})
public ResponseEntity<UserDto> copyUserWithTypeOld(@RequestParam UserTypeEnum type) {
    ...
}  

@GetMapping(value = "/users/copy", params = {"type=NEW"})
public ResponseEntity<UserDto> copyUserWithTypeNew(@RequestParam UserTypeEnum type) {
    ...
}

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

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