Invalid exception format.

I use Jersey 2.5.1 as an implementation of jax-rs and I use Moxy as a JSON serializer. I configured Jersey to print validation errors for output in web.xml.

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

This works fine as validation errors are returned in plain text (text / plain). The problem is that I would like to receive validation error messages in JSON format and, in accordance with the DHL documentation , in order to do this, the JSON provider must be configured for this. As far as I know, Moxy is configured as a JSON provider when its dependencies are bound to a classpath. Unfortunately, my validation errors are not returned in JSON format (application / json). What could be wrong. Do I need to configure extra bits?

Ps when I debug a ValidationExceptionMapper, the following code returns a Variant object with the text / plain text type

 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() ) ); } 
+6
source share
1 answer

As I mentioned in the comment, the reason for not returning the JSON format was due to the fact that I sent the header:

  Accept: * / * 

It must be installed:

  Accept: application / json 

for proper operation.

+8
source

All Articles