在spring-boot应用程序中,我想使用通用的异常处理程序来捕获和处理com.fasterxml.jackson.databind.exc.InvalidFormatException
-Exception,并在遇到一个异常时返回400
。
是,不调用该异常的异常处理程序。而是返回一个偶然的500错误。
我想念的是什么?这是否与InvalidFormatException是嵌套异常有关?我该如何解决?
代码-异常处理程序
@ControllerAdvice(basePackages = "packe.path.to.rest.controllers")
public class GeneralExceptionHandler {
// .. other working exception handlers...
@ExceptionHandler(InvalidFormatException.class)
public ResponseEntity<ErrorMessage> processJsonInvalidFormatException(InvalidFormatException e) {
log.error("Invalid Json Format Exceptioon occurred");
HttpStatus status = HttpStatus.BAD_REQUEST;
ErrorMessage msg = new ErrorMessage(
status.value(),
status.getReasonPhrase(),
e.getMessage()
);
return new ResponseEntity(msg, status);
}
日志显示
org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of some.packe.property.A from number value (123): index value outside legal index range [0..2]
at [Source: java.io.PushbackInputStream@5599410f; line: 2, column: 2] (through reference chain: some.path.to.my.input.body.class["someState"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of path.again... from number value (123): index value outside legal index range [0..2]
at [Source: java.io.PushbackInputStream@5599410f; line: 2, column: 2] (through reference chain: and.again...)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:208) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]