当前,来自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" }
我正在寻找一种方法来消除响应中的“ exception”属性。有没有办法做到这一点?
如有关错误处理的文档中所述,您可以提供自己的bean,该bean实施ErrorAttributes以控制内容。
ErrorAttributes
一个简单的方法就是继承DefaultErrorAttributes。例如:
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; } }; }