javaspringrestspring-mvcjax-rs

WebApplicationException Equivalent In Spring Rest


I am rewriting some JAX-RS code as spring. However, I am not sure what to do with these exceptions.

if(file == null) {
    throw new WebApplicationException(404);
}

Does spring have a similar concept? I know about ResponseEntity.status(HttpStatus.NOT_FOUND).body("Not Found"); However, it is extra work to convert everything. Currently, the endpoints just return strings, not response objects.

Thanks,


Solution

  • Judging from what you want to achieve, I assume returning a specific error code without modifying the signature of all the methods.

    For Spring Web you could use the ResponseStatusException to achieve the same.

    if(file == null) {
        throw new ResponseStatusException(HttpStatus.NOT_FOUND);
    }
    

    Something along those lines.