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