Modify default JSON error response from Spring Boot Rest Controller

As described in the documentation on error handling, you can provide your own bean that implements ErrorAttributes to take control of the content.

An easy way to do that is to subclass DefaultErrorAttributes. For example:

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

   };
}

Leave a Comment