Where to throw a customized 404 error in knitwear when HttpServer cannot find a resource

I want to configure a 404 response so that the server (and not me) throws out every time it cannot find the requested resource (or throw its own WebApplicationException view if it is possible to check if the requested resource is present in one application. Probably, the list of resources is somewhere stored?). please do not direct me to solutions that suggest extending WebApplicationException, because even so, my problem is when to throw it? when the resource is not found! but how to express this need for a jersey frame.

+6
source share
1 answer

Jersey throws javax.ws.rs.NotFoundException when it cannot find the endpoint. Just use the exception mapper to convert it to the answer of your choice:

 import javax.ws.rs.NotFoundException; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; @Provider public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> { public Response toResponse(NotFoundException exception) { return Response.status(Response.Status.NOT_FOUND) .entity("No such resource") .build(); } } 
+9
source

Source: https://habr.com/ru/post/977500/


All Articles