【发布时间】: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