-
1. Re: WELD and HibernateValidator - ConstraintViolations always reported as "text/plain".
mkouba Nov 24, 2015 3:27 AM (in response to chrisjr)Hi Chris,
I think this is more a question for RESTEasy team. When looking at the ResteasyViolationExceptionMapper it seems only "text/plain" is supported for
ConstraintViolationException
thrown by Hibernate Validator's ValidationInterceptor (used to validate CDI beans deployed as JAX-RS resources). JAX-RS spec also does not require any special handling here, see also 7.6 Validation and Error Reporting: "...the exact content and format of this entity is beyond the scope of this specification...". -
2. Re: WELD and HibernateValidator - ConstraintViolations always reported as "text/plain".
chrisjr Nov 24, 2015 4:05 AM (in response to mkouba)Hi Martin,
When I examined the ResteasyViolationExceptionMapper, the problem appeared to be that ResteasyViolationException.getAccept() was always returning "text/plain" rather than the "application/json" which I was explicitly passing via the HTTP Accept header. So as I understand it, I think Resteasy should be executing this block of code:
// Check standard media types. MediaType mediaType = getAcceptMediaType(exception.getAccept()); if (mediaType != null) { builder.type(mediaType); builder.entity(new ViolationReport(exception)); return builder.build(); }
where getAcceptMediaType(List<MediaType> accept) is where Resteasy implements the logic described in Chapter 51. Validation
I also think that any effort to convince the Resteasy developers that it was their problem would go something like this:
Resteasy: Does it work with just Resteasy and Hibernate Validator?
Me: Yes, it does. (See resteasy-violator.tar)Resteasy: Well, then...!
Cheers,
Chris
-
3. Re: WELD and HibernateValidator - ConstraintViolations always reported as "text/plain".
mkouba Nov 24, 2015 6:03 AM (in response to chrisjr)The question is whether the exception mapper recieves an instance of ResteasyViolationException. For CDI beans it might be ConstraintViolationException so that it always returns MediaType.TEXT_PLAIN (see also the last line in
toResponse()
method).WRT RESTEasy - first I'm not really sure it is implementable. And you probably cannot even force them to implement this as it's not required by the spec. However, I believe the team would be helpful if you ask them for help.
I wonder whether implementing a custom exception mapper would help...
-
4. Re: WELD and HibernateValidator - ConstraintViolations always reported as "text/plain".
chrisjr Nov 24, 2015 9:30 AM (in response to mkouba)Martin Kouba wrote:
And you probably cannot even force them to implement this as it's not required by the spec.My point is that it is documented Resteasy behaviour rather than part of the JAX-RS specification:
If any constraint violations are detected, Resteasy will return a report in one of a variety of formats. If one of "application/xml" or "application/json" occur in the "Accept" request header, Resteasy will return an appropriately marshalled instance of org.jboss.resteasy.api.validation.ViolationReport
See Chapter 51. Validation. Basically, including WELD is breaking this behaviour somehow.
And yes, I have considered writing a custom exception mapper as a last resort.
-
6. Re: WELD and HibernateValidator - ConstraintViolations always reported as "text/plain".
mkouba Nov 25, 2015 2:26 AM (in response to chrisjr)Thanks for info. I've added a comment.