diy*_*diy 5 java spring exception spring-mvc
我正在使用 Spring @ControllerAdvice 来处理异常
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = { DataIntegrityViolationException.class})
@ResponseBody
public ResponseEntity<String> unknownException(Exception ex, WebRequest req) {
return new ResponseEntity<>(ex.getCause().getMessage(), new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当异常发生时(当我通过 swagger 发送请求时),我没有收到预期的异常消息,但是:
{"error": "no response from server"}
Response Code : 0
Response Body : No Content
Run Code Online (Sandbox Code Playgroud)
我可以在调试模式下清楚地看到调用了注释的方法@ExceptionHandler。
我已经尝试过方法返回类型、@ResponseBody注释@ResponseStatus和其他一些想到的东西,但似乎当我返回ResponseEntity没有主体的 a 时,我只会得到一些非空响应,例如
ResponseEntity.noContent().build()
Run Code Online (Sandbox Code Playgroud)
或者
ResponseEntity.ok().build()
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我得到正确的 http 代码和一些标头
请告诉我我做错了什么
先感谢您
UPD
我继续进行实验,这是对我有用的解决方案。它非常接近答案之一 - 我会将其标记为已接受
简而言之,我刚刚创建了自己的 dto 类,用我感兴趣的异常详细信息填充了实例,并直接返回了我的代码
@ExceptionHandler(value = { DataIntegrityViolationException.class})
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public ExceptionDetailHolder unknownException(Exception ex, WebRequest req) {
final Throwable cause = ex.getCause();
return new ExceptionDetailHolder("Error interacting with the database server",
cause.getClass() + ":" + cause.getMessage(),
cause.getCause().getClass() + ":" + cause.getCause().getMessage()
);
}
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
private class ExceptionDetailHolder {
private String message;
private String exceptionMessage;
private String innerExceptionMessage;
}
Run Code Online (Sandbox Code Playgroud)
结果(还显示了评论者询问的 ex.getMessage 和 ex.getCause().getMessage() 的内容):
{
"message": "Error interacting with the database server",
"exceptionMessage": "class org.hibernate.exception.ConstraintViolationException:could not execute statement",
"innerExceptionMessage": "class com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Column 'allow_copay_payments' cannot be null"
}
Run Code Online (Sandbox Code Playgroud)
我处理异常的方式如下,我找到特定的异常,然后在本例中创建我自己的类对象 ValidationErrorDTO,然后填充该类中的必填字段(ValidationErrorDTO):
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ResponseEntity<ValidationErrorDTO> processValidationIllegalError(HttpMessageNotReadableException ex,
HandlerMethod handlerMethod, WebRequest webRequest) {
Throwable throwable = ex.getMostSpecificCause();
ValidationErrorDTO errorDTO = new ValidationErrorDTO();
if (throwable instanceof EnumValidationException) {
EnumValidationException exception = (EnumValidationException) ex.getMostSpecificCause();
errorDTO.setEnumName(exception.getEnumName());
errorDTO.setEnumValue(exception.getEnumValue());
errorDTO.setErrorMessage(exception.getEnumValue() + " is an invalid " + exception.getEnumName());
}
return new ResponseEntity<ValidationErrorDTO>(errorDTO, HttpStatus.BAD_REQUEST);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5625 次 |
| 最近记录: |