Example usage for com.fasterxml.jackson.databind.exc UnrecognizedPropertyException getCause

List of usage examples for com.fasterxml.jackson.databind.exc UnrecognizedPropertyException getCause

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.exc UnrecognizedPropertyException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:io.confluent.rest.validation.JacksonMessageBodyProvider.java

@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
    try {/*from  w  w  w .  j ava2s  .c  o  m*/
        return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
    } catch (UnrecognizedPropertyException e) {
        throw ConstraintViolations.simpleException("Unrecognized field: " + e.getPropertyName());
    } catch (JsonMappingException e) {
        // This needs to handle 2 JSON parsing error cases. Normally you would expect to see a
        // JsonMappingException because the data couldn't be parsed, but it can also occur when the
        // raw JSON is valid and satisfies the validation constraint annotations, but an exception is
        // thrown by the entity during construction. In the former case, we want to return a 400
        // (Bad Request), in the latter a 422 (Unprocessable Entity) with a useful error message. We
        // don't want to expose just any exception message via the API, so this code specifically
        // detects ConstraintViolationExceptions that were thrown *after* the normal validation
        // checks, i.e. when the entity Java object was being constructed.
        Throwable cause = e.getCause();
        if (cause instanceof ConstraintViolationException) {
            throw (ConstraintViolationException) cause;
        }
        throw e;
    }
}