简体   繁体   English

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

[英]Modify default JSON error response from Spring Boot Rest Controller

Currently the error response from spring boot contains the standard content like below:目前 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"
}

I am looking for a way to get rid of the "exception" property in the response.我正在寻找一种方法来摆脱响应中的“异常”属性。 Is there a way to achieve this?有没有办法实现这一目标?

As described in the documentation on error handling , you can provide your own bean that implements ErrorAttributes to take control of the content.错误处理文档中所述,您可以提供自己的 bean 来实现ErrorAttributes以控制内容。

An easy way to do that is to subclass DefaultErrorAttributes .一个简单的方法是将DefaultErrorAttributes子类化。 For example:例如:

@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;
        }

   };
}

If there is empty message text in json when you encounter exception, you can be hit by changed behavior in spring boot 2.3.0 .如果遇到异常时 json 中的消息文本为空,则可能会被spring boot 2.3.0 中的更改行为击中。 If this is the case, just change your server.error.include-message property to always .如果是这种情况,只需将您的server.error.include-message属性更改为always

Following answer is totally derived from Andy Wilkinson's answer (which uses web.reactive classes)以下答案完全源自Andy Wilkinson 的答案(使用web.reactive类)
- It includes web.servlet based classes. - 它包括基于web.servlet的类。
- Spring boot 2.2.4.RELEASE - 弹簧靴 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;
            }
        };
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM