6

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()
                    )
            );
        }
Bart
  • 301
  • 5
  • 12
  • What is the content-type of your response (text/plain or something else)? Can you show us your resource method you're invoking and what are you sending in accept header from client to server? – Michal Gajdos Jan 07 '14 at 08:47
  • I started grabbing information that you asked for and I noticed that a plugin that I was using for testing didn't add header: Accept: application/json. After adding it I am getting response in JSON format. – Bart Jan 07 '14 at 23:32

1 Answers1

8

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.

Bart
  • 301
  • 5
  • 12