从@RestControllerAdvice @ExceptionHandler方法返回 DTO



我在@RestControllerAdvice中有以下@ExceptionHandler方法:

@ExceptionHandler(BusinessException.class)
public ResponseEntity<Object> onException(BusinessException exception, WebRequest request) {
Locale locale = request.getLocale();
String message = messageSource.getMessage(exception.errorCode.toString(),
exception.arguments, locale);
ErrorDto errorDto = new ErrorDto(
exception.errorCode,
exception.arguments,
message
);
return new ResponseEntity(errorDto, new HttpHeaders(), HttpStatus.CONFLICT);
}

ErrorDto是:

public class ErrorDto {
ErrorCode errorCode;
String[] arguments = new String[]{};
String message;
public ErrorDto(ErrorCode errorCode, String[] arguments, String message){
this.errorCode = errorCode;
this.arguments = arguments;
this.message = message;
}
}

但是当抛出BusinessException时,我得到的回应是:

{
"timestamp": 1500459833663,
"status": 500,
"error": "Internal Server Error",
"exception": "com.ciet.app.exceptions.BusinessException",
"message": "No message available",
"path": "/CietApi/errorCodeTest"
}

为什么我在响应中看不到ErrorDto

ErrorDto字段需要公开或需要有公共获取者。

最新更新