REST JAX-RS error handler does not capture errors


I currently have a REST service with JAX-RS in tomcat. Now I want the typical errors of 400,405,500 error, etc to be returned in JSON format and not HTML as it does in normal.

Then I've been basing myself on link to create an error handler.

This is a generic handler.

public class GenericExceptionMapper implements ExceptionMapper<WebApplicationException> {

 public Response toResponse(final WebApplicationException exception) {
    final ErrorResponse response = new ErrorResponse("Generic excepton", "503",
    return Response.ok().entity(response).build();

And this is a specific one (even if it's identical)

public class NotAllowedExceptionMapper implements ExceptionMapper<NotAllowedException> {

 public Response toResponse(final NotAllowedException exception) {
    final ErrorResponse response = new ErrorResponse(exception.getMessage(), "503",
    return Response.ok().entity(response).build();


Then I have already prepared to test it, so when you receive a request, automatically throw an exception.

public Response list(@Context final UriInfo uriInfo, @Context final HttpHeaders httpHeaders,
        @PathParam(USER_ID) final Integer userId, @QueryParam("offset") final Integer offset,
        @QueryParam(LIMIT) final Integer limit, @PathParam(OPERATION_LABEL) final String oper) {

    throw new NotAllowedException("test");
    /*Response response;
    if (oper.equalsIgnoreCase(RELOAD_OP)) {
        response = reload();
    } else {

        response = listImplementation(uriInfo, httpHeaders, userId, offset, limit, oper);
    return response;*/

Launched the following request

GET http://localhost:8080/webservice/persons

Putting a breaking point in WS waiting for the call I see that it reaches the throw new NotAllowedException("test"); but does not get to enter any of the two handlers that I have put and returns the classic html code.

<body><h1>Estado HTTP 405 – Method Not Allowed</h1><hr class="line" /><p><b>Tipo</b> Informe de estado</p><p><b>mensaje</b> Method Not Allowed</p><p><b>descripción</b> El método HTTP especificado no está permitido para el recurso requerido.</p><hr class="line" /><h3>Apache Tomcat/8.0.53</h3></body>

Any idea why I do not get into my handlers?

asked by Cadeq 12.12.2018 в 11:06

1 answer


I have already discovered the cause of the error. Everything that is put is correct. I had the problem in web.xml and in the location of my handlers

This is the content of my web.xml with the problem already solved


The problem was that as I had the mappers in a package other than web services, these were not recognized by jersey.

Everything was solved by adding for jersey to recognize my mappers.

I hope that this solution will be useful to people because I found this condition pure coincidence and nowhere did I see that it was necessary.

answered by 12.12.2018 / 16:02