我使用@ ControllerAdvice,@ ErrorHandler和@ResponseStatus注释来返回一些错误信息。我确定处理程序方法已执行(我已在debuger下检查过它。)但我的ErrorInfo对象被Tomcat HTML错误页面覆盖。
@ExceptionHandler(value = ServiceExecutionException.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Internal Server Error")
ErrorInfo handleServiceError(HttpServletRequest request, HttpServletResponse response, Exception e) {
return new ErrorInfo(request.getRequestURL().toString(), e.getLocalizedMessage());
}
这是类似的问题,但它没有包含正确的答案,因为我试图避免使我的代码复杂化。 Disable all default HTTP error response content in Tomcat
答案 0 :(得分:0)
尝试一下:
@ExceptionHandler(ServiceExecutionException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public ResponseEntity<ErrorInfo> handleServiceError(ServiceExecutionException ex,
HandlerMethod handlerMethod, WebRequest webRequest) {
String url = ((ServletWebRequest)webRequest).getRequest().getRequestURL().toString();
ErrorInfo errorInfo = new ErrorInfo(url, ex.getLocalizedMessage());
return new ResponseEntity<ErrorInfo>(errorInfo, HttpStatus.INTERNAL_SERVER_ERROR);
}