json - 修改来自 Spring Boot Rest Controller 的默认 JSON 错

目前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"
}

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

最佳答案

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

   };
}

关于json - 修改来自 Spring Boot Rest Controller 的默认 JSON 错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29106637/

相关文章:

python - 类型错误 : b'1' is not JSON serializable

java - 最新的 org.json

java - 是否有将 Java POJO 转换为 JSON 和 XML 的库?

python - 将输出序列化为 JSON - ValueError : Circular refe

c# - Json.NET:反序列化嵌套字典

javascript - typescript 对象序列化?

c# - 无法使用 Json.net 序列化具有复杂键的字典

javascript - 使用 Asp.Net MVC 和 KnockoutJS 处理日期

json - JSON 格式的 Amazon S3 响应?

java - 使用 jackson 删除 JSON 元素