What is a better way to design an API on a @Controller method when request parameters are mutually exclusive?
Let's say that there is an API to provide a List of Users that matches the request parameters.
Code is:
public ResponseEntity getList(@RequestParam(required = false) Integer userId,
@RequestParam(required = false) User.Type userType,
@RequestParam(required = false) Integer age) {
List<User> userList = null;
if (userId != null) {
//logic
userList = getUserByUserId()
} else if (userType != null) {
//logic
userList = getUserByType()
} else if (age != null) {
//logic
userList = getListByAge()
} else {
userList = getAllWithoutCondition();
}
return ResponseEntity.ok(userList);
}
Here is the point:
The user cannot query with more than one request parameter. Only one request parameter or no request parameter is valid (only one of userId, age, or type should exist in a request).
I am not sure what the better way to design an API for this situation is. Can you give me some advice?