【问题标题】:Modify default JSON error response from Spring Boot Rest Controller修改来自 Spring Boot Rest Controller 的默认 JSON 错误响应
【发布时间】:2015-05-20 07:50:57
【问题描述】:

目前spring boot的错误响应包含如下标准内容:

{
   "timestamp" : 1426615606,
   "exception" : "org.springframework.web.bind.MissingServletRequestParameterException",
   "status" : 400,
   "error" : "Bad Request",
   "path" : "/welcome",
   "message" : "Required String parameter 'name' is not present"
}

我正在寻找一种方法来摆脱响应中的“异常”属性。有没有办法做到这一点?

【问题讨论】:

    标签: json spring rest spring-boot


    【解决方案1】:

    documentation on error handling 中所述,您可以提供自己的实现ErrorAttributes 的bean 来控制内容。

    一个简单的方法是继承DefaultErrorAttributes。例如:

    @Bean
    public ErrorAttributes errorAttributes() {
        return new DefaultErrorAttributes() {
            @Override
            public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
                Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
                // Customize the default entries in errorAttributes to suit your needs
                return errorAttributes;
            }
    
       };
    }
    

    【讨论】:

    • 感谢您的回复!我曾尝试使用 ErrorAttributes,但似乎无法对该单元进行测试。请参阅 [stackoverflow.com/questions/29120948/… 知道如何做到这一点吗?
    • 如何确定父类?
    • @MatejJ 抱歉,我不确定我是否理解您的要求。 “确定父类”是什么意思?
    • 你提到你需要继承DefaultErrorAttributes。我不确定spring boot在哪里调用super.getErrorAttributes
    【解决方案2】:

    如果遇到异常时json中有空的消息文本,可以被changed behavior in spring boot 2.3.0命中。如果是这种情况,只需将您的 server.error.include-message 属性更改为 always

    【讨论】:

    • github.com/spring-projects/spring-boot/wiki/… 默认错误页面内容的更改 默认情况下,错误消息和任何绑定错误不再包含在默认错误页面中。这降低了向客户泄露信息的风险。 server.error.include-messageserver.error.include-binding-errors 可用于分别控制消息的包含和绑定错误。支持的值为always, on-param, and never。使用 always 将修复 spring boot 2.3.0 版本中的空消息
    【解决方案3】:

    以下答案完全源自Andy Wilkinson's 答案(使用web.reactive 类)
    - 它包括基于web.servlet 的类。
    - Spring Boot 2.2.4.RELEASE

    ExceptionHandlerConfig.java

    package com.example.sample.core.exception;
    
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
    import org.springframework.boot.web.servlet.error.ErrorAttributes;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.context.request.WebRequest;
    
    @Configuration
    public class ExceptionHandlerConfig {
    
        //private static final String DEFAULT_KEY_TIMESTAMP = "timestamp";
        private static final String DEFAULT_KEY_STATUS = "status";
        private static final String DEFAULT_KEY_ERROR = "error";
        private static final String DEFAULT_KEY_ERRORS = "errors";
        private static final String DEFAULT_KEY_MESSAGE = "message";
        //private static final String DEFAULT_KEY_PATH = "path";
    
        public static final String KEY_STATUS = "status";
        public static final String KEY_ERROR = "error";
        public static final String KEY_MESSAGE = "message";
        public static final String KEY_TIMESTAMP = "timestamp";
        public static final String KEY_ERRORS = "errors";
    
        //
    
        @Bean
        public ErrorAttributes errorAttributes() {
            return new DefaultErrorAttributes() {
    
                @Override
                public Map<String ,Object> getErrorAttributes(
                    WebRequest webRequest
                    ,boolean includeStackTrace
                ) {
                    Map<String ,Object> defaultMap
                        = super.getErrorAttributes( webRequest ,includeStackTrace );
    
                    Map<String ,Object> errorAttributes = new LinkedHashMap<>();
                    // Customize.
                    // For eg: Only add the keys you want.
                    errorAttributes.put( KEY_STATUS, defaultMap.get( DEFAULT_KEY_STATUS ) );                    
                    errorAttributes.put( KEY_MESSAGE ,defaultMap.get( DEFAULT_KEY_MESSAGE ) );
    
                    return errorAttributes;
                }
            };
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 2017-08-08
      • 2015-04-05
      • 2017-03-08
      • 2015-09-04
      • 1970-01-01
      • 2015-11-09
      • 2022-02-03
      相关资源
      最近更新 更多