[英]ResponseEntity<ErrorResponse> not getting returned as custom HTTP response body
ErrorResponse class:错误响应 class:
public class ErrorResponse {
private String errorCode;
private String errorMessage;
...
}
Controller Advice: Controller 建议:
@ExceptionHandler(BaseException.class)
public ResponseEntity<Object> handleBaseException(BaseException exception) {
ErrorResponse res = new ErrorResponse(exception.getErrorCode().toString(),exception.getMessage());
return new ResponseEntity<>(res, HttpStatus.NOT_FOUND);
}
Http response: Http 响应:
{
"timestamp": 1661865968618,
"status": 500,
"error": "Internal Server Error",
"message": "custom message",
"path": "...."
}
Expected Http response:预期的 Http 响应:
{
"errorCode": "...",
"errorMessage": "..."
}
Can anyone help me with the return statement of @controlleradvice such that I can get the expected Http response?任何人都可以帮助我处理@controlleradvice 的返回语句,以便我可以获得预期的 Http 响应吗?
@ExceptionHandler(BaseException.class)
public ResponseEntity<?> handleBaseException(HttpServletRequest request, BaseException exception) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(new ErrorResponse(HttpStatus.NOT_FOUND, exception.getMessage()), String.valueOf(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)));
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.