gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:50:08 26 4
gpt4 key购买 nike

目前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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com