下面列出了com.fasterxml.jackson.databind.JsonMappingException#Reference ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
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();
}
@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();
}
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);
}