0

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;
    }
  • 2
    Possible duplicate of [With Spring 3.0, can I make an optional path variable?](https://stackoverflow.com/questions/4904092/with-spring-3-0-can-i-make-an-optional-path-variable) – Ori Marko Dec 03 '18 at 13:23
  • What do you want to return if the user is not found in the DB? – A_C Dec 03 '18 at 13:27
  • I want to return HttpStatus.NOT_FOUND –  Dec 03 '18 at 13:29

3 Answers3

1

In simple terms, if you want to use Optional for checking availability of user in the database:

public ResponseEntity<User> getUser(@PathVariable("id") int id) {
    User User= this.userService.getUserById(id);
    ResponseEntity<User> responseEntity = new ResponseEntity<>(HttpStatus.NOT_FOUND);
    if (Optional.ofNullable(user).isPresent()) {
        responseEntity = new ResponseEntity<>(user, HttpStatus.OK);
    }

    return responseEntity;
}
A_C
  • 905
  • 6
  • 18
0

Path parameters can't be made optional, you'll have to map 2 URLs to your get controller method. Try below:

@RequestMapping(method=GET, value={"/", "/{id}"})
 public ResponseEntity<User> getUser(@PathVariable Optional<Integer> id) {

    if(!id.isPresent()){
      return ResponseEntity.notFound().build();
    }
    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;
}

This solution requires Spring 4.1+ and Java 1.8.

Aditya Narayan Dixit
  • 2,105
  • 11
  • 23
0

For me this an elegant way to do it, normally id is Long type

public ResponseEntity<User> getUser(@PathVariable ("id") Long id) {
            return Optional.of(this.userService.getUserById(id))
                    .map(u -> new ResponseEntity<>(u, HttpStatus.OK))
                    .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
        }
Jose
  • 124
  • 9
  • Optional.of() will give a null pointer if user is null...right? Better use Optional.ofNullable() – A_C Dec 04 '18 at 07:19
  • No, it will return new ResponseEntity<>(HttpStatus.NOT_FOUND) with .orElse() method – Jose Dec 04 '18 at 15:43
  • I think it will be a NullPointerException based on Java Docs – A_C Dec 04 '18 at 16:25
  • Check this out, it makes sense https://stackoverflow.com/questions/31696485/why-use-optional-of-over-optional-ofnullable – Jose Dec 04 '18 at 18:10