users@jax-rs-spec.java.net

[jax-rs-spec users] Re: ExceptionMapper for ConstraintViolationException

From: Santiago Pericas-Geertsen <Santiago.PericasGeertsen_at_oracle.com>
Date: Wed, 15 Apr 2015 08:50:51 -0400

Hi Christian,

 The intent of the BV integration in JAX-RS is to minimize the BV code apps need to write “by hand”. If JAX-RS implementations throw custom exception types for BV errors, there’s little sense in mandating a default (yet overridable) VE mapper for all JAX-RS implementations. The idea is for these BV exceptions to propagate to JAX-RS applications.

— Santiago

> On Apr 15, 2015, at 1:21 AM, Christian Kaltepoth <christian_at_kaltepoth.de> wrote:
>
> Hey all,
>
> I've a question for you.
>
> Imagine a resource class which uses Bean Validation annotations for defining constraints. See this example:
>
> https://github.com/chkal/jaxrs-bv/blob/master/src/main/java/de/chkal/jaxrs/MyResource.java#L12 <https://github.com/chkal/jaxrs-bv/blob/master/src/main/java/de/chkal/jaxrs/MyResource.java#L12>
>
> Does the spec require that a user should be able to define a custom ExceptionMapper for ConstraintViolationExceptions to handle validation failures? See this ExceptionMapper as an example:
>
> https://github.com/chkal/jaxrs-bv/blob/master/src/main/java/de/chkal/jaxrs/MyExceptionMapper.java#L9 <https://github.com/chkal/jaxrs-bv/blob/master/src/main/java/de/chkal/jaxrs/MyExceptionMapper.java#L9>
>
> I'm asking this because the scenario described above doesn't work in RESTEasy. It looks like RESTEasy throws some custom exception type instead of the raw ConstraintViolationException which basically means that the ExceptionMapper isn't applied.
>
> The relevant sentences from the spec are:
> Section 7.6: JAX-RS implementations MUST provide a default exception mapper [...] for javax.validation.ValidationException [...]
> Section 7.6: [...] applications can provide their own exception mappers and, consequently, customize the default mapper described above.
> Section 4.4: When choosing an exception mapping provider to map an exception, an implementation MUST use the provider whose generic type is the nearest superclass of the exception.
> However, there is IMO nothing in the spec requiring that the JAX-RS implementation throws the raw Bean Validation exception type.
>
> Any thoughts on this?
>
> Christian
>
> PS: You will find the example application which is deployable to any JavaEE7 compatible container here:
>
> https://github.com/chkal/jaxrs-bv <https://github.com/chkal/jaxrs-bv>
>
>
> --
> Christian Kaltepoth
> Blog: http://blog.kaltepoth.de/ <http://blog.kaltepoth.de/>
> Twitter: http://twitter.com/chkal <http://twitter.com/chkal>
> GitHub: https://github.com/chkal <https://github.com/chkal>
>