简体   繁体   English

如何在 spring rest api 中编程 404 错误?

[英]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?我试图抛出 spring rest api 的 404 错误,但它不会接受我给出的异常?

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<?原因:不存在类型变量 X 的实例,因此 ResponseStatusException 符合 Supplier<? extends X>延伸 X>

This is my first time making a rest API, am I supposed to change the exception in some way for this to work?这是我第一次制作 rest API,我是否应该以某种方式更改异常以使其工作?

orElseThrow method expects a Supplier : orElseThrow方法需要一个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...注意使用orElseThrow( () -> new...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM