Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

question from:https://stackoverflow.com/questions/65945614/how-to-program-404-error-in-spring-rest-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
127 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...