我有一个异常处理程序控制器,在这里捕获HttpMessageNotReadableException,如下所示:
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
protected ErrorMessage handleJsonException(final HttpMessageNotReadableException ex, final HttpServletRequest request)
{
if (ex.getCause() instanceof JsonParseException)
{
// some code
}
if (ex .getCause() instanceof JsonMappingException)
{
// some code
}
}
我有不同的原因来发布文章,并使用格式错误的JSON (JSON文本中缺少第一个双引号)
{firstName":"abc","lastName":"xyz"}
后JsonParseException
PUT - JsonMappingException
我认为这两者都应该有相同的原因"JsonParseException“的语法是错误的。
有谁能提出为什么春天给出的"JsonMappingException".不同的PUT
发布于 2013-11-07 21:21:09
为了解决这类问题,我发现本文-> http://www.jayway.com/2013/02/03/improve-your-spring-rest-api-part-iii/有一些解决方法,比如使用"getMostSpecificCause“。我读它是为了解决我的问题。
发布于 2016-07-25 15:56:50
试试这个//ex -> HttpMessageNotReadableException
Throwable throwable = ex.getCause();
JsonMappingException jsonMappingException = ((JsonMappingException) throwable);
// import 'InvalidFormatException' from com.fasterxml.jackson.databind.exc package
List<JsonMappingException.Reference> references = ((InvalidFormatException)throwable).getPath();
for (JsonMappingException.Reference reference : references) {
if (reference.getFieldName() != null) {
field += reference.getFieldName() + ".";
}
}
String message = jsonMappingException.getOriginalMessage();
https://stackoverflow.com/questions/18120705
复制相似问题