[英]Spring boot (with spring MVC) stopped sending body in default error responses (probably after version update)
I have a more or less simple CRUD Spring MVC application with a minimum of business logic. 我有一个或多或少简单的CRUD Spring MVC应用程序,具有最少的业务逻辑。 I'm therefore kinda heavily depending on default exception handling simply by annotating my exception classes with @ResponseStatus to control status codes with just a couple of @ExceptionHandler handlers for a few special cases.
因此,我非常依赖于默认的异常处理,只需通过使用@ResponseStatus注释我的异常类来控制状态代码,只需几个特殊情况就可以使用几个@ExceptionHandler处理程序。 Recently I've noticed that in cases where I'm depending on default handling my responses have empty bodies, as opposed to previously containing a message from the exception (custom @ExceptionHandler work properly).
最近我注意到,在我依赖于默认处理的情况下,我的响应具有空体,而不是之前包含来自异常的消息(自定义@ExceptionHandler正常工作)。
I've tried looking into white-label error page but all I can find are how-tos on the customization or disabling, nothing regarding its misconfiguration or anything similar. 我已经尝试过查看白标错误页面但我能找到的只是定制或禁用的方法,没有关于它的错误配置或任何类似的东西。 I'm suspecting it's caused by updating to newer spring boot and/or spring cloud dependencies, but can't find anything which would explain it in release notes
我怀疑它是由更新到更新的spring boot和/或spring cloud依赖项引起的,但在发行说明中找不到任何可以解释它的东西
I'm expecting a JSON containing the error message as opposed to the current completely empty body. 我期待一个包含错误消息的JSON,而不是当前完全空的主体。
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler({Exception.class})
public ResponseEntity handler1(Exception e){
System.out.println("error occurred!");
ResponseStatus status = AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class);
HttpStatus res=null;
if(status!=null){
res=status.value()!=null?status.value():(status.code()!=null?status.code():null);
}
ResponseResult result = new ResponseResult();
result.setErrorCode(res!=null?res.value():500);
result.setMsg(e.getMessage());
return new ResponseEntity(result,res==null?HttpStatus.INTERNAL_SERVER_ERROR:res);
}
}
@Setter
@Getter
public class ResponseResult {
private String msg;
private int errorCode;
}
This way it returns JSON containing your error msg and the http response status is 500 or the status you annotated on your Exception class. 这样它返回包含错误消息的JSON,http响应状态为500或您在Exception类上注释的状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.