Reputation: 41
Im trying to throw a 404 error for a spring rest api when nothing is found for an assignment, but it wont accept the exception im giving?
Warehouse warehouse = warehouseRepository.findById(warehouseId).orElseThrow(new ResponseStatusException(HttpStatus.NOT_FOUND, "No warehouses with specified ID were found"));
its giving me this compilation error:
reason: no instance(s) of type variable(s) X exist so that ResponseStatusException conforms to Supplier<? extends X>
This is my first time making a rest API, am I supposed to change the exception in some way for this to work?
Upvotes: 0
Views: 711
Reputation: 94
orElseThrow
method expects a Supplier
:
Warehouse warehouse = warehouseRepository.findById(warehouseId)
.orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND,
"No warehouses with specified ID were found"));
Note the use of orElseThrow(
() ->
new ...
Upvotes: 3