从@RestControllerAdvice @ExceptionHandler方法返回DTO

时间:2017-07-19 10:26:40

标签: spring spring-boot

我在for(i=0;i<=(l.length-1);i++)中有以下@ExceptionHandler方法:

@RestControllerAdvice

@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" }

1 个答案:

答案 0 :(得分:1)

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