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

时间:2015-03-17 18:08:54

标签: json spring rest 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"
}

我正在寻找摆脱"异常"的方法。响应中的财产。有没有办法实现这个目标?

3 个答案:

答案 0 :(得分:43)

documentation on error handling中所述,您可以提供自己的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;
        }

   };
}

答案 1 :(得分:24)

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

答案 2 :(得分:3)

以下答案完全来自Andy Wilkinson's答案(使用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;
            }
        };
    }
}