【问题标题】:Configure ResponseEntity body while throwing exception在抛出异常时配置 ResponseEntity 主体
【发布时间】:2018-02-28 17:57:56
【问题描述】:

我创建了一个通用异常 DTO,它也扩展了 RuntimeException。通过这种方式,可以在应用程序和 DTO 中使用它。问题是当我将 DTO 应用于 ResponseEntity 构造函数时。然后我得到了额外的整个堆栈跟踪:

{
"cause": null,
"stackTrace": [
    {
        "methodName": "handleConstraintViolations",
        "fileName": "ProductExceptionHandler.java",
        "lineNumber": 66,
        "className": "api.product.infrastructure.ProductExceptionHandler",
        "nativeMethod": false
    },
    {
        "methodName": "invoke0",
        "fileName": "NativeMethodAccessorImpl.java",
        "lineNumber": -2,
        "className": "sun.reflect.NativeMethodAccessorImpl",
        "nativeMethod": true
    },
    {
        "methodName": "invoke",
        "fileName": "NativeMethodAccessorImpl.java",
        "lineNumber": 62,
        "className": "sun.reflect.NativeMethodAccessorImpl",
        "nativeMethod": false
    },
    {
        "methodName": "invoke",
        "fileName": "DelegatingMethodAccessorImpl.java",
        "lineNumber": 43,
        "className": "sun.reflect.DelegatingMethodAccessorImpl",
        "nativeMethod": false
    },
    {
        "methodName": "invoke",
        "fileName": "Method.java",
        "lineNumber": 498,
        "className": "java.lang.reflect.Method",
        "nativeMethod": false
    },
    {
        "methodName": "doInvoke",
        "fileName": "InvocableHandlerMethod.java",
        "lineNumber": 205,
        "className": 
"org.springframework.web.method.support.InvocableHandlerMethod",
        "nativeMethod": false
    },
....
"status": "UNPROCESSABLE_ENTITY",
"message": "Constraint violation(s)",
"errors": [
    "name size must be between 1 and 2147483647"
],
"localizedMessage": "Constraint violation(s)",
"suppressed": []
}

我的异常 DTO 类:

@Getter
@EqualsAndHashCode(callSuper = true)
public class ProductException extends RuntimeException implements Serializable {

private final HttpStatus status;
private final String message;
private final String[] errors;

public ProductException(HttpStatus status, String message, String... errors) {
    this.status = status;
    this.message = message;
    this.errors = errors;
}

public ProductException(HttpStatus status, String message, Set<ConstraintViolation<?>> constraintViolation) {
    this.status = status;
    this.message = message;
    this.errors = constraintViolation.stream()
            .map(violation -> violation.getPropertyPath().toString() + " " + violation.getMessage())
            .toArray(String[]::new);
}
}

我这样称呼它:

 @ExceptionHandler(TransactionSystemException.class)
public ResponseEntity<ProductException> handleConstraintViolations(TransactionSystemException tse) {
    ConstraintViolationException cve = (ConstraintViolationException) tse.getMostSpecificCause();
    ProductException productException = new ProductException(HttpStatus.UNPROCESSABLE_ENTITY, "Constraint violation(s)", cve.getConstraintViolations());
    return new ResponseEntity<>(productException, new HttpHeaders(), productException.getStatus());

我有两个解决方案,首先是创建适配器,将我的异常转移到一个非扩展的 RuntimeException,另一个想法是在我的异常中创建一个类似 .toBody() 的方法。最好的办法是阻止 ResponseEntity 添加“原因”、“堆栈跟踪”等属性。有什么想法吗?

【问题讨论】:

    标签: java spring exception exception-handling


    【解决方案1】:

    首先是创建适配器,将我的异常转移到一个非扩展的 RuntimeException

    在我看来,这是您最好的选择。通过这样做,您将可以很好地控制将获得异常的客户端可用的信息(在您的应用程序中或作为 DTO,如您所说)。

    在您的@ExceptionHandler 中,您只需绑定需要返回或不返回的有用信息(并可能记录应该记录的内容)。

    【讨论】:

    • 我现在有适配器,但它有点笨拙,我的意思是只为此目的创建另一个类。无论如何谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 2013-05-24
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 2017-05-11
    • 2022-06-16
    相关资源
    最近更新 更多