java - 在抛出异常时配置 ResponseEntity 主体

标签 java spring exception

我创建了一个通用异常 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 添加“原因”、“堆栈跟踪”等属性。有什么想法吗?

最佳答案

first is creating adapter which transfers my Exception to one's non extending RuntimeException

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

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

关于java - 在抛出异常时配置 ResponseEntity 主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49035804/

相关文章:

Java - 增强的 For 循环永无止境

java - 如何查找函数的 Java SDK 版本

java - 使用浏览器取消请求

java - 使用 Spring 的 ResourceDatabasePopulator 导入 MySQL 表定义转储

java - JPA - 只获取父项,而不获取子项

c++ - C++编程中内存分配较多时如何处理异常

java - 有没有一种简单的方法可以让android studio自动添加版权声明?

java - Spring Integration Http 休息服务

c# - 异常消息是否应该全局化

java - 编译java程序filenotfound异常