javajax-rsbean-validationexceptionmapper

JAXRS/Jersey 2 - Validation errors does not invoke the ExceptionMapper


using latest Jersey (2.22.1), I've successfully created my custom validators for various needs. But my custom ExceptionMapper (registered as a provider in web.xml) is not invoked when a ConstraintViolationException occurs, altough it is defined as an ExceptionMapper<Throwable>.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="mywebapp" version="2.5">

    <display-name>Some Name - Webapp</display-name>

    [...]    

    <servlet>
        <servlet-name>jersey_v2-servlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>
                com.myfirm.web.rest.providers.DefaultExceptionMapper,
                com.myfirm.web.rest.endpoints.XxxEndpoint,
                com.myfirm.web.rest.endpoints.XxyEndpoint,
                com.myfirm.web.rest.endpoints.XyzEndpoint
            </param-value>
        </init-param>

        <init-param>
            <param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
            <param-value>true</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey_v2-servlet</servlet-name>
        <url-pattern>/rest/1.0/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>jersey_v2-servlet</servlet-name>
        <url-pattern>/rest/latest/*</url-pattern>
    </servlet-mapping>

    [...]

</web-app>

DefaultExceptionMapper

@Provider
public class DefaultExceptionMapper implements ExceptionMapper<Throwable> {
    private static final Logger LOG = LoggerFactory.getLogger(DefaultExceptionMapper.class);

    @Override
    public Response toResponse(Throwable caughtException) {
        LOG.warn("Exception caught in the REST layer", caughtException);

        Throwable original = caughtException;

        // some business logic to convert the exception to a response

        // log & return the response
        Response response = status(status).entity(entity).build();
        return response;
    }

    @XmlRootElement
    public static class Error {
        @XmlElement
        public String type;

        @XmlElement
        public String message;

        @XmlElement
        public String translationKey;
    }
}

Using my debugger, I can see in the class org.glassfish.jersey.server.ServerRuntime, line 596, that the resolved mapper is not mine, but is a org.glassfish.jersey.server.validation.internal.ValidationExceptionMapper.

How can I tell Jersey to use my DefaultExceptionMapper in the case of a ConstraintViolationException ?

PS: I've tried options suggested here: ExceptionMapper not invoked if receiving invalid JSon with no luck.


Solution

  • Defining the mapper as a implements ExceptionMapper<ConstraintViolationException> made it somehow take precedence over other mapper registered for the same exception type.

    I've ended up with 2 exception mappers, one for every exceptions, the other for ConstraintViolationException, both extending the same abstract class.