I extended DefaultErrorAttributes to have a better control over error responses, because I need different http status codes for different exceptions. but response status code does not change even though I set related attribute to WebRequest:
@Component
public class CustomErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace);
errorAttributes.put("status", 404); //404 is example
webRequest.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, 404, 0); // 404 is example
return errorAttributes;
}
}
Ignores 404 and returns 500 to client
It is working in spring boot 2.1.x but not in 2.2.x (2.2.6 tested)
I tried adding this attribute on httpServletRequest but this doesn't work either
Comment From: wilkinsona
This is a duplicate of https://github.com/spring-projects/spring-boot/issues/18952. As noted on that issue, relying on a side-effect of manipulating the requests attributes is discouraged. Please see the opening description of #18952 for an alternative approach.