Java Code Examples for com.fasterxml.jackson.databind.JsonMappingException#Reference

The following examples show how to use com.fasterxml.jackson.databind.JsonMappingException#Reference . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapperUtils.java    From act-platform with ISC License 6 votes vote down vote up
/**
 * Helper function to construct a string representation of a property path from a JsonMappingException
 * (similar to a property path from a ConstraintViolationException).
 *
 * @param exception JsonMappingException
 * @return String representation of property path
 */
static String printPropertyPath(JsonMappingException exception) {
  if (CollectionUtils.isEmpty(exception.getPath())) return "UNKNOWN";

  String propertyPath = "";
  for (JsonMappingException.Reference ref : exception.getPath()) {
    if (ref.getFieldName() != null) {
      if (!propertyPath.isEmpty()) {
        propertyPath += ".";
      }

      propertyPath += ref.getFieldName();
    } else {
      propertyPath += String.format("[%d]", ref.getIndex());
    }
  }

  return propertyPath;
}
 
Example 2
Source File: JsonMappingExceptionMapper.java    From heroic with Apache License 2.0 6 votes vote down vote up
private String constructPath(final JsonMappingException e) {
    final StringBuilder builder = new StringBuilder();

    final Consumer<String> field = name -> {
        if (builder.length() > 0) {
            builder.append(".");
        }

        builder.append(name);
    };

    for (final JsonMappingException.Reference reference : e.getPath()) {
        if (reference.getIndex() >= 0) {
            builder.append("[" + reference.getIndex() + "]");
        } else {
            field.accept(reference.getFieldName());
        }
    }

    if (e.getCause() instanceof Validation.MissingField) {
        final Validation.MissingField f = (Validation.MissingField) e.getCause();
        field.accept(f.getName());
    }

    return builder.toString();
}
 
Example 3
Source File: JsonContentConversionExceptionMapper.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public Response toResponse(InvalidFormatException ex) {
    // log the error
    logger.info(String.format("%s. Returning %s response.", ex, Response.Status.BAD_REQUEST));

    if (logger.isDebugEnabled()) {
        logger.debug(StringUtils.EMPTY, ex);
    }

    String value = ex.getValue().toString();
    String propName = "field";

    if (ex.getPath() != null && !ex.getPath().isEmpty()) {
        JsonMappingException.Reference path = ex.getPath().get(ex.getPath().size() - 1);
        if (path != null) {
            propName = path.getFieldName();
        }
    }

    String errorMessage = "The provided " + propName + " value '" + sanitizeMessage(value) + "' is not of required type " + ex.getTargetType();

    logger.error(errorMessage);

    return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).type("text/plain").build();
}
 
Example 4
Source File: UpdateMessagePatchValidator.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Optional<String> firstFieldFrom(List<JsonMappingException.Reference> references) {
    if (references == null) {
        throw new IllegalArgumentException("references");
    }
    return references.stream()
            .map(JsonMappingException.Reference::getFieldName)
            .map(MessageProperties.MessageProperty::valueOf)
            .findFirst()
            .map(MessageProperties.MessageProperty::asFieldName);
}