Remember when we first launched our application without adding a controller? We got a funny Whitelabel Error Page output.
Error handling is a lot trickier than it looks, especially when you don't have a web.xml
configuration file and want your application to be portable across web servers. The good news is that Spring Boot takes care of that for us! Let's look at ErrorMvcAutoConfiguration
:
ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) @ConditionalOnWebApplication // Ensure this loads before the main WebMvcAutoConfiguration so that the error View is // available @AutoConfigureBefore(WebMvcAutoConfiguration.class) @Configuration public class ErrorMvcAutoConfiguration implements EmbeddedServletContainerCustomizer, Ordered { @Value("${error.path:/error}") private String errorPath = "/error"; @Autowired private ServerProperties properties; @Override public int getOrder() { return 0; } @Bean @ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT) public DefaultErrorAttributes errorAttributes() { return new DefaultErrorAttributes(); } @Bean @ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT) public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) { return new BasicErrorController(errorAttributes); } @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage(this.properties.getServletPrefix() + this.errorPath)); } @Configuration @ConditionalOnProperty(prefix = "error.whitelabel", name = "enabled", matchIfMissing = true) @Conditional(ErrorTemplateMissingCondition.class) protected static class WhitelabelErrorViewConfiguration { private final SpelView defaultErrorView = new SpelView( "<html><body><h1>Whitelabel Error Page</h1>" + "<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>" + "<div id='created'>${timestamp}</div>" + "<div>There was an unexpected error (type=${error}, status=${status}).</div>" + "<div>${message}</div></body></html>"); @Bean(name = "error") @ConditionalOnMissingBean(name = "error") public View defaultErrorView() { return this.defaultErrorView; } // If the user adds @EnableWebMvc then the bean name view resolver from // WebMvcAutoConfiguration disappears, so add it back in to avoid disappointment. @Bean @ConditionalOnMissingBean(BeanNameViewResolver.class) public BeanNameViewResolver beanNameViewResolver() { BeanNameViewResolver resolver = new BeanNameViewResolver(); resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10); return resolver; } } }
What does this piece of configuration do?
DefaultErrorAttributes
, which exposes helpful error information via special attributes such as the status, error code, and associated stack trace.BasicErrorController
bean, which is an MVC controller in charge of displaying the error page we've seen.error.whitelable.enabled
to false in our configuration file, application.properties
.error.html
, for example. This is what the condition ErrorTemplateMissingCondition
checks.We'll see how to properly handle errors later in this book.
As far as encoding is concerned, the very simple HttpEncodingAutoConfiguration
function will handle it by providing Spring's CharacterEncodingFilter
class. It is possible to override the default encoding ("UTF-8") with spring.http.encoding.charset
and disable this configuration with spring.http.encoding.enabled
.
3.14.142.194