修改Spring Boot Rest Controller的默认JSON错误响应

Mar*_*rco 32 rest spring json spring-boot

目前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"
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种摆脱响应中"异常"属性的方法.有没有办法实现这个目标?

And*_*son 45

错误处理文档中所述,您可以提供自己的bean来实现ErrorAttributes控制内容.

一个简单的方法是子类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;
        }

   };
}
Run Code Online (Sandbox Code Playgroud)


Lub*_*ubo 31

如果遇到异常时 json 中的消息文本为空,则可能会被spring boot 2.3.0 中的更改行为击中。如果是这种情况,只需将您的server.error.include-message属性更改为always

  • https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes 对默认错误页面内容的更改默认错误页面中不再包含错误消息和任何绑定错误默认情况下。这降低了向客户泄露信息的风险。`server.error.include-message` 和 `server.error.include-binding-errors` 可用于分别控制消息和绑定错误的包含。支持的值有“always、on-param 和 never”。使用 `always` 将修复 spring boot 2.3.0 版本中的空消息 (4认同)

Vin*_*ssh 5

以下答案完全源自Andy Wilkinson 的答案(使用web.reactive类)
- 它包括web.servlet基于类。
- 弹簧靴 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;
            }
        };
    }
}
Run Code Online (Sandbox Code Playgroud)