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!
Even though the above answer is correct I would go for an even better more functional solution.
Instead of calling Optional#isPresent
to check whether the Grocery
object is present I would something like:
public ResponseEntity<Object> get(@PathVariable final Integer id) {
return groceryRepository.findOne(id)
.map(g -> ResponseEntity.ok(g))
.orElse(ResponseEntity.status(HttpStatus.NOT_FOUND).body(MessageUtil.parse(MSG_404_GROCERY, id + ""));
}
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 + "")));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With