Spring JPA Create ResponseEntity from findById()

Mr. Wrong :

In a spring boot application, I have an endpoint that returns a HTTP 200 response with an object if it exists, or a HTTP 404 reponse if not. Using spring-boot-starter-parent 1.5.7 I used to do that like this:

@Autowired private GroceryRepository groceryRepository;

@RequestMapping(value = "/get/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public ResponseEntity<Object> get(@PathVariable final Integer id) {
    final Grocery grocery = groceryRepository.findOne(id);
    if (null == grocery) {
        return ResponseEntity
                .status(HttpStatus.NOT_FOUND)
                .body(MessageUtil.parse(MSG_404_GROCERY, id + ""));
    }
    return ResponseEntity.ok(grocery);
}

GroceryRepository extends JpaRepository<Grocery, Integer>

In spring-boot-starter-parent 2.0.0, findOne() has disappeared from JpaRepository, and findById() returns an Optional. I'm struggling a bit on how to 'port' the above code. The following doesn't build, since it has an unexpected return type:

groceryRepository.findById(id).ifPresent(grocery -> {
    return ResponseEntity.ok(grocery);
});

Can someone tell me what would be the proper way of returning the right Response?

Thanks for any help!

Ousmane D. :

ifPresent has a void return type so performing return ResponseEntity.ok(grocery); will not work as you've already witnessed.

That said, Optional has an isPresent() method which you can use:

Optional<Grocery> groceryOptional = groceryRepository.findById(id);
if(groceryOptional.isPresent()){
    return ResponseEntity.ok(groceryOptional.get());
}else {
    return ResponseEntity
            .status(HttpStatus.NOT_FOUND)
            .body(MessageUtil.parse(MSG_404_GROCERY, id + ""));
}

Another approach would be:

Optional<Grocery> groceryOptional = groceryRepository.findById(id);
 return groceryOptional.map(e -> ResponseEntity.ok(e))
                       .orElse(ResponseEntity
                              .status(HttpStatus.NOT_FOUND)
                              .body(MessageUtil.parse(MSG_404_GROCERY, id + "")));

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=473435&siteId=1