I am using Jersey 2.5.1 as jax-rs implementation and I use Moxy as JSON serialiser. I configured Jersey to print validation errors to output in web.xml.
<init-param>
<param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
<param-value>true</param-value>
</init-param>
That works fine as validation errors are returned in plain text (text/plain). The problem is that I would like to get validation error messages in JSON format and according to Jersey documentation in order to do this a JSON provider must be configured to this. As far as I know it Moxy is configured as a JSON provider when its dependencies are attached to classpath. Unfortunately my validation errors are not returned in JSON (application/json) format. What can be wrong. Do I have to configure extra bits?
P.s. when I debug ValidationExceptionMapper following code returns Variant object with media type text/plain
if (property != null && Boolean.valueOf(property.toString())) {
final List<Variant> variants = Variant.mediaTypes(
MediaType.TEXT_PLAIN_TYPE,
MediaType.TEXT_HTML_TYPE,
MediaType.APPLICATION_XML_TYPE,
MediaType.APPLICATION_JSON_TYPE).build();
final Variant variant = request.get().selectVariant(variants);
if (variant != null) {
response.type(variant.getMediaType());
} else {
// default media type which will be used only when none media type from {@value variants} is in accept
// header of original request.
// could be settable by configuration property.
response.type(MediaType.TEXT_PLAIN_TYPE);
}
response.entity(
new GenericEntity<List<ValidationError>>(
ValidationHelper.constraintViolationToValidationErrors(cve),
new GenericType<List<ValidationError>>() {}.getType()
)
);
}
As I mentioned in the comment the reason for not returning JSON format was due to the fact that I was sending header:
Accept: */*
It must be set to:
Accept: application/json
in order work properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With