0

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?

1 Answer 1

3

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 ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.