asdaf rwgafe
asdaf rwgafe

Reputation: 41

How to program 404 error in spring rest api?

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

Answers (1)

vmargerin
vmargerin

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

Related Questions