下面列出了com.fasterxml.jackson.core.JsonProcessingException#getLocation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Response fromJsonProcessingException(JsonProcessingException e) {
StringBuilder msgBuilder = new StringBuilder();
if (e.getOriginalMessage() != null) {
msgBuilder.append(e.getOriginalMessage());
} else {
msgBuilder.append("JSON processing error");
}
JsonLocation location = e.getLocation();
if (location != null) {
msgBuilder.append(" location: [line: ").append(location.getLineNr())
.append(", column: ").append(location.getColumnNr()).append(']');
}
ErrorResponse errorResponse = ErrorResponse.newError(HttpServletResponse.SC_BAD_REQUEST, msgBuilder.toString())
.clientRequest(httpServletRequest)
.serverContext()
.exceptionContext(e)
.build();
return Response.status(Status.BAD_REQUEST).entity(errorResponse).build();
}
private static Map<String,Object> readFragment(File file, String ext) throws IOException {
ObjectMapper mapper = new ObjectMapper("json".equals(ext) ? new JsonFactory() : new YAMLFactory());
TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
try {
Map<String, Object> ret = mapper.readValue(file, typeRef);
return ret != null ? ret : new HashMap<>();
} catch (JsonProcessingException e) {
throw new JsonMappingException(String.format("[%s] %s", file, e.getMessage()), e.getLocation(), e);
}
}
private Map<String, Object> appendJacksonErrorDetails(JsonProcessingException cause) {
Map<String, Object> out = new TreeMap<>();
JsonLocation location = cause.getLocation();
if (location != null) {
out.put("errorLocation", "line: " + location.getLineNr() + ", column: " + location.getColumnNr());
if (location.getSourceRef() != null && location.getSourceRef() instanceof String) {
out.put("document", location.getSourceRef());
}
}
if (cause instanceof JsonMappingException) {
JsonMappingException mappingEx = (JsonMappingException) cause;
if (mappingEx.getPathReference() != null) {
out.put("pathReference", mappingEx.getPathReference());
}
if (cause instanceof InvalidFormatException) {
InvalidFormatException formEx = (InvalidFormatException) cause;
if (formEx.getTargetType() != null) {
out.put("targetType", formEx.getTargetType().getName());
}
} else if (cause instanceof PropertyBindingException) {
PropertyBindingException bindingEx = (PropertyBindingException) cause;
if (bindingEx.getPropertyName() != null) {
out.put("property", bindingEx.getPropertyName());
out.put("knownProperties", bindingEx.getKnownPropertyIds());
}
}
}
return out;
}
public static JsonNode build(final JsonProcessingException e,
final boolean crlf)
{
final JsonLocation location = e.getLocation();
final ObjectNode ret = JsonNodeFactory.instance.objectNode();
/*
* Unfortunately, for some reason, Jackson botches the column number in
* its JsonPosition -- I cannot figure out why exactly. However, it does
* have a correct offset into the buffer.
*
* The problem is that if the input has CR/LF line terminators, its
* offset will be "off" by the number of lines minus 1 with regards to
* what JavaScript sees as positions in text areas. Make the necessary
* adjustments so that the caret jumps at the correct position in this
* case.
*/
final int lineNr = location.getLineNr();
int offset = (int) location.getCharOffset();
if (crlf)
offset = offset - lineNr + 1;
ret.put(LINE, lineNr);
ret.put(OFFSET, offset);
// Finally, put the message
ret.put(MESSAGE, e.getOriginalMessage());
return ret;
}
public static JsonReadException fromJackson(JsonProcessingException ex)
{
String message = ex.getMessage();
// Strip off location.
int locPos = message.lastIndexOf(" at [Source");
if (locPos >= 0) {
message = message.substring(0, locPos);
}
return new JsonReadException(message, ex.getLocation());
}
public MalformedJsonException(JsonProcessingException cause) {
super(cause);
this.location = cause.getLocation();
}
public static SwaggerError newJsonError(JsonProcessingException exception) {
int line = (exception.getLocation() != null) ? exception.getLocation().getLineNr() : 1;
return new SwaggerError(line, IMarker.SEVERITY_ERROR, 0, exception.getMessage());
}
/**
* {@inheritDoc}
*/
@Override
public Response toResponse(JsonProcessingException exception) {
Throwable throwable = exception;
while (throwable != null) {
if (throwable instanceof PersistenceException) {
return exceptionMappers.get().findMapping(throwable).toResponse(throwable);
}
throwable = throwable.getCause();
}
logger.debug("Json Processing error", exception);
String message = exception.getOriginalMessage();
String desc = null;
String source = null;
if (mode.isDev()) {
desc = IOUtils.getStackTrace(exception);
JsonLocation location = exception.getLocation();
if (location != null) {
source = "line: " + location.getLineNr() +
", column: " + location.getColumnNr();
} else {
source = exception.getStackTrace()[0].toString();
}
}
ErrorMessage errorMessage = ErrorMessage.fromStatus(Response.Status.BAD_REQUEST.getStatusCode());
errorMessage.setThrowable(exception);
errorMessage.setCode(Hashing.murmur3_32().hashUnencodedChars(exception.getClass().getName()).toString());
errorMessage.addError(new Result.Error(
errorMessage.getCode(),
message != null ? message : exception.getMessage(),
desc,
source
));
return Response.status(errorMessage.getStatus())
.entity(errorMessage)
.type(ExceptionMapperUtils.getResponseType())
.build();
}