Reputation: 5455
I have this UserController with a GET users method.
public class UserController {
private final static Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping
public List<User> getUsers() throws ResourceNotFoundException {
try {
return userService.getUsers();
} catch (Exception e) {
LOGGER.error(e.getMessage());
return ResponseEntity.internalServerError().build();
}
}
}
The problem is with the return type. Because I'm returning a list of users, I set that as the return type. But since it can also return a ResponseEntity
, my IDE complains about it. The quick fix it offers is to set the return type to ResponseEntity
but then of cause it will complain about the fact this method can return a List<User>
type.
OR
How do I specify both possible return types?
Upvotes: 2
Views: 115
Reputation: 77226
You have two options:
ResponseEntity<E>
and use something like return ResponseEntity.ok(users)
.@ControllerAdvice
to centralize your exception handling so that you write your handling code in one place and apply it consistently.Upvotes: 4
Reputation: 42531
You can return the ResponseEntity<List<User>>
from the controller method:
It basically wraps the actual result, but allows more fine-grained controll over the return statuses.
Sometimes you don't really have to throw an exception and delegate the exception handling to the controller advice. If you don't want to catch the possible exceptions - fine, if the exception is not thrown in the controller itself but comes from the service classes called from the controller, also you can use controller advice. But if you can handle everything in controller (like altering the return type of the response) - you can still do this with response entity, at least it seems to be a viable option.
So technically you should do:
@GetMapping
public ResponseEntity<List<User>> getUsers() throws ResourceNotFoundException {
try {
return ResponseEntity.ok(userService.getUsers()); // wrapped!!!
} catch (Exception e) {
LOGGER.error(e.getMessage());
return ResponseEntity.internalServerError().build();
}
}
Upvotes: 2
Reputation: 3936
You cannot return more than one types from the method. Instead of it, throw the exception and caller will need to handle it.
@GetMapping
public List<User> getUsers() throws ResourceNotFoundException {
try {
return userService.getUsers();
} catch (ResourceNotFoundException e) {
LOGGER.error(e.getMessage());
throw new CustomException("Error while searching for user");
}
}
OR
@GetMapping
public List<User> getUsers() throws ResourceNotFoundException {
return userService.getUsers();
}
Upvotes: 2