我有以下异常处理程序:
@Log4j2
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity handleException(Exception e) {
logExceptionWithPath("Unhandled general exception", e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
@ResponseBody
@ExceptionHandler({ValidationException.class})
public ResponseEntity<ErrorResponse> handleBindException(ValidationException ex) {
logExceptionWithPath("Validation exception", ex);
return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}
@ResponseBody
@ExceptionHandler({BindException.class})
public ResponseEntity<ErrorResponse> handleBindException(BindException ex) {
logExceptionWithPath("Bind exception", ex);
return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}
@ResponseBody
@ExceptionHandler(AmbiguousTermException.class)
public ResponseEntity<ErrorResponse> handleAmbiguousTermException(AmbiguousTermException ex) {
logExceptionWithPath("AmbiguousTermException", ex);
return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}
此代码应包装异常,并使用正确的状态代码返回ResponseEntity。
但是,我总是这样:
{
"timestamp": 1560445348350,
"status": 500,
"error": "Internal Server Error",
"message": "I/O error on POST request for \"******": Error code:400 Message: ; nested exception is java.io.IOException: Error code:400 Message: ",
"path": "*****"
}
在日志中,我总是有由logExceptionWithPath生成的消息,因此ExceptionHandler似乎工作正常,但是在某个地方有IOException。
对于每个异常(和每个@ExceptionHandler),此响应都相似
在我的应用程序和春季的一些日志中:
2019-06-13 17:26:00,237 DEBUG *:102 - assertTermsUnequivocal
2019-06-13 17:26:00,237 DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:403 - Using @ExceptionHandler public org.springframework.http.ResponseEntity<*.ErrorResponse> *.GlobalExceptionHandler.handleAmbiguousTermException(*.AmbiguousTermException)
2019-06-13 17:26:00,238 ERROR *.GlobalExceptionHandler:92 - AmbiguousTermException for path: http://*/search
here stack trace from AmbiguousTermException
2019-06-13 17:26:00,239 DEBUG org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor:268 - Using 'application/json', given [text/plain, application/json, application/cbor, application/*+json, */*] and supported [application/json, application/*+json, application/json, application/*+json, application/cbor]
2019-06-13 17:26:00,239 DEBUG org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor:90 - Writing [*.ErrorResponse@17bd7832]
2019-06-13 17:26:00,240 DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:143 - Resolved [*.AmbiguousTermException: terms are ambiguous]
2019-06-13 17:26:00,240 DEBUG org.springframework.web.servlet.DispatcherServlet:1130 - Completed 400 BAD_REQUEST
全部来自有趣的日志。也许这根本没有连接到Spring,而是服务器正在运行? 无论如何,如果您有任何不起作用的线索,请留下答案或评论。
答案 0 :(得分:0)
您必须将“响应”状态添加到类似@ResponseStatus(HttpStatus.NOT_FOUND)
的方法中
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST) // Added this annotation
@ExceptionHandler(AmbiguousTermException.class)
public ResponseEntity<ErrorResponse> handleAmbiguousTermException(AmbiguousTermException ex) {
logExceptionWithPath("AmbiguousTermException", ex);
return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}