Java 自定义错误处理程序返回大量调试消息

Java 自定义错误处理程序返回大量调试消息,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,我想创建一个自定义异常处理程序,它返回一个带数据的结构化JSON响应。我试过这个: @ExceptionHandler(BaseException.class) public ResponseEntity<ErrorResponseDTO> handleException(BaseException ex) { LOG.error(ex.getMessage(), ex.getCause()); ErrorResponse errorEntry

我想创建一个自定义异常处理程序,它返回一个带数据的结构化JSON响应。我试过这个:

@ExceptionHandler(BaseException.class)
    public ResponseEntity<ErrorResponseDTO> handleException(BaseException ex) {
        LOG.error(ex.getMessage(), ex.getCause());
        ErrorResponse errorEntry = new ErrorResponse();
        errorEntry.setTitle(ex.getTitle());
        errorEntry.setCode(ex.getErrorCode());
        HttpStatus httpStatus = ErrorDetail.getHttpStatusBasedOnErrorCode(ex.getErrorCode());
        errorEntry.setStatus(httpStatus.value());
        errorEntry.setDetail(ex.getMessage());
        errorEntry.setExtra(ex.getExtra());

        ErrorResponseDTO errorResponse = new ErrorResponseDTO();
        errorResponse.setErrors(Arrays.asList(errorEntry));

        return new ResponseEntity<ErrorResponseDTO>(errorResponse, httpStatus);
    }
我只想得到这个结果:

{
    "errors": [
        {          
            "status": 404,
            "code": "1000",
            "title": "Not found",
            "detail": "item Not found",
            "extra": {
                "detail": "values are not found"
            }
        }
    ]
}
你知道我为什么会得到这些错误数据,以及我如何解决这个问题吗?

看看这个问题

供日后参考的相关代码:

...
import lombok.Getter;
import lombok.Setter;
...

@Setter
@Getter
public class ErrorResponse extends Throwable {

    private int status;

    private String code;
...
这解释了为什么json响应中会出现一个错误

创建新的
ErrorResponse
对象时:

ErrorResponse errorEntry = new ErrorResponse();
还将调用可丢弃的
的no args costructuror(
ErrorResponse
的超类),它用堆栈的当前状态填充
stackTrace
数组:

构造一个新的throwable,其详细信息为null

调用fillInStackTrace()方法来初始化堆栈跟踪 新创建的可丢弃文件中的数据

要获得所需结果,
ErrorResponse
不应扩展
Throwable

@Setter
@Getter
public class ErrorResponse {

    private int status;

    private String code;
...
您能从ErrorResponse.java类中删除“extends Throwable”并尝试一下吗?
@Setter
@Getter
public class ErrorResponse {

    private int status;

    private String code;
...