javaspring-bootresthttp-request-parameters

Multiple endpoints with the same path but with different @RequestParam values


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) {
    ...
}

Solution

  • 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) {
        ...
    }