Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom exception mapping for bean validation on TomEE 7?

Context

We use a javax.ws.rs.ext.ExceptionMapper<Exception> annotated as @javax.ws.rs.ext.Provider to handle all exceptions. Internally this ExceptionMapper is distinguishing between different types of exceptions to determine what information to reveal to the client.

In the case of the javax.validation.ConstraintViolationException, we return additional information about which field was invalid and why.

Problem

We just switched from TomEE 1.7.2 JAX-RS to TomEE 7.0.0-SNAPSHOT webprofile.

With TomEE 1.7.2 JAX-RS we used the openejb.jaxrs.providers.auto=true system property, our ExceptionMapper was automatically found and used.

With TomEE 7.0.0-SNAPSHOT webprofile the property is no longer necessary to benefit from auto discovery.

However the org.apache.cxf.jaxrs.validation.ValidationExceptionMapper is also discovered and now acts as the preferred ExceptionMapper for the javax.validation.ConstraintViolationException. Our own ExceptionMapper does not run, and the client therefore gets no information about what went wrong during validation.

Our own ExceptionMapper<Exception> still handles all other exceptions.

What I already tried

"duplicate" the specialized ExceptionMapper

I placed my own javax.ws.rs.ext.ExceptionMapper<javax.validation.ConstraintViolationException> next to my resources, hoping that it takes precedence over the CXF one.

Still the org.apache.cxf.jaxrs.validation.ValidationExceptionMapper takes precedence.

Update: it turned out that this indeed does the trick. I don't know why my initial test didn't work.

Disable the ValidationExceptionMapper via system.properties

In the changelog of TomEE 7.0.0-SNAPSHOT I noticed

TOMEE-1336 Support classname.activated = true/false for auto discovered providers

Looking at the corresponding changeset I was hopeful that I could just disable the org.apache.cxf.jaxrs.validation.ValidationExceptionMapper by adding org.apache.cxf.jaxrs.validation.ValidationExceptionMapper.activated=false to our system.properties.

This remained without effect.

Questions

  • Is this CXF or TomEE behaviour?
  • How do we configure which ExceptionMapper takes precedence?
like image 630
Schroenser Avatar asked Nov 17 '15 14:11

Schroenser


1 Answers

Makes some time now but think it is needed by spec but you can disable it by setting cxf.jaxrs.skip-provider-scanning=true.

It completely disables auto providers including the scanned ones but then you can control the one you want in openejb-jar.xml - surely the best and safer solution IMHO cause otherwise you depend a lot of the libs and container setup you use.

There is no priority afaik cause the exception hierarchy is used.

edit: missed a part: you need to impl ExceptionMapper{ValidationException} otherwise CXF one has higher priority than your own one (Exception is less specific) edit 2: https://issues.apache.org/jira/browse/TOMEE-1656 for the activated support

like image 143
Romain Manni-Bucau Avatar answered Nov 05 '22 04:11

Romain Manni-Bucau