【发布时间】:2019-02-25 02:14:41
【问题描述】:
我正在尝试在我的 Spring Boot 应用程序中实现一些自定义异常处理程序,这些处理程序将能够处理自定义异常并显示适当的消息和状态代码。
我的问题:即使响应正文是根据我的自定义处理程序获得的 http 状态 = 500。
代码:
@ControllerAdvice
public class MyExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({ BadRequestValidationFailureException.class, Exception.class })
public ResponseEntity<Object> handleAll(Exception ex, WebRequest request) {
ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getMessage());
return new ResponseEntity<Object>( apiError, new HttpHeaders(), HttpStatus.BAD_REQUEST );
}
并抛出异常:
throw new BadRequestValidationFailureException( "ERROR_CODE", "THIS IS THE MESSAGE" );
输出是:
{
"timestamp": "2018-09-20T17:44:01.502Z",
"status": 500,
"error": "Internal Server Error",
"exception": "com.hotstar.payment.exception.BadRequestValidationFailureException",
"message": "[ ERROR_CODE ] THIS IS THE MESSAGE",
"path": "/my/api/path"
}
奇怪的是http响应状态是500。
请帮忙。
【问题讨论】:
标签: spring spring-boot exception-handling