简体   繁体   English

Spring启动(使用spring MVC)在默认错误响应中停止发送正文(可能在版本更新后)

[英]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.

相关问题 在spring boot中发送REST响应的最佳方式 - Best way of sending REST responses in spring boot 自定义 Spring 开机报错响应码不改默认正文 - Customize Spring Boot error response code without changing the default body 更新到新版本后无法编译Spring Boot项目 - Cannot Compile Spring Boot Project after Update to New Version Spring 引导中请求正文的默认类型是什么 - What is the default type of request body in Spring boot Spring 不返回默认验证错误响应 - Spring not returning default validation error responses Spring 启动应用程序在从 2.37 版本迁移到 2.4.1 版本后停止工作,它在 windows 上工作,但在 linux 机器上不工作 - Spring boot app stopped working after migration from 2.37 to 2.4.1 version, it is working on windows but not on linux machine 具有 Spring 安全性的 Spring 引导在一段时间后自动停止工作 - Spring boot with Spring security automatically stopped working after some time 次要版本更新后的Spring Security异常:“请确保在共享的ApplicationContext中配置了Spring Security和Spring MVC” - Spring Security exception after minor version update: “Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext” Spring开机启动器父版更新 - Spring boot starter parent version update 自定义正文错误消息 - Spring启动REST - Customize body error message - Spring boot REST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM