How should I check, that I receive an entity from DB and return correct response? I use restController. I want to check that I receive a user from DB or not. If I found the user, I want to return the user and HttpStatus.OK, if not - HttpStatus.NOT_FOUND.
public ResponseEntity<User> getUser(@PathVariable("id") int id) {
User User= this.userService.getUserById(id);
ResponseEntity<User> responseEntity = new ResponseEntity<>(HttpStatus.NOT_FOUND);
if (Objects.nonNull(user)) {
responseEntity = new ResponseEntity<>(user, HttpStatus.OK);
}
return responseEntity;
}