1

I have an API path GET /users/copy. I can have two APIs with the same /users/type path but with different sets of RequestParams using the following construction:

@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?

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) {
    ...
}
htshame
  • 6,599
  • 5
  • 36
  • 56
  • I don't know why you want to do this, but this is not a good pattern. You can do if else inside method. Still you want to use, you can do `/users/copy/type/old` `/users/copy/type/new`. – Eklavya Aug 26 '20 at 16:15
  • Yes, I understand that my question is peculiar. But adding suffixes is not an option for me – htshame Aug 26 '20 at 16:17
  • If you want fixed param then you use suffix is better. Or use if-else inside method. – Eklavya Aug 26 '20 at 16:19
  • I can't add suffixes and I'd very much like to avoid additional if-else constructions. Otherwise I wouldn't have had this question – htshame Aug 26 '20 at 16:21
  • If you are taking request param and doing static behavior then there is no meaning to use request param actually. And if you want to filter User using userType you can query dynamically in the repository (guessing data fetching) – Eklavya Aug 26 '20 at 16:25
  • It makes sense. But the question remains, at least out of the sporting interest – htshame Aug 26 '20 at 16:31
  • 2
  • My goodness! I never thought something that trivial would work! Thanks!!! – htshame Aug 26 '20 at 16:55

1 Answers1

0

Actually, the answer was in the question.

In order to have different APIs endpoints with the same path and the same set of @RequestParam, but with different @RequestParam values you need to specify params attribute as such:

@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) {
    ...
}
Stephane
  • 11,836
  • 25
  • 112
  • 175
htshame
  • 6,599
  • 5
  • 36
  • 56
  • Could you elaborate and explain what is this type attribute doing and how to send the request ? Thanks! – Stephane Aug 25 '22 at 12:45
  • 1
    `type` is just a parameter name, it doesn't have anything to do with request mapping. Requests would be `GET /users/copy?type=NEW` or `GET /users/copy?type=OLD` – htshame Aug 25 '22 at 12:48