Skip to content
Advertisement

Spring Boot get JSON Error representation in the ErrorController

Spring Boot provides errors in the json format like this:

JavaScript

Is it possible for me to obtain this error inside the ErrorController and to proceed with it?

JavaScript

Is it inside the HttpServletResponse or maybe something else?

Advertisement

Answer

The default error attributes are extracted from a WebRequest using the ErrorAttributes bean.

Spring already provided a default implementation of this bean, the DefaultErrorAttributes bean.

You can have this bean injected by the container, as any usual bean, to your custom /error controller implementation so you can use it:

JavaScript

Update (by @Igorz)

As of Spring Boot version 2.3.0

JavaScript

has been deprecated in favor of:

JavaScript

Static factory ErrorAttributeOptions.defaults() can be used as default ErrorAttributeOptions argument of above method.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement