《精通Spring MVC 4》——1.7 错误与转码配置
本节书摘来自异步社区《精通Spring MVC 4》一书中的第1章,第1.7节,作者:【美】Geoffroy Warin著,更多章节内容可以访问云栖社区“异步社区”公众号查看
1.7 错误与转码配置还记得在没有添加控制器的时候,第一次启动应用吗?当时看到了一个有意思的“Whitelabel Error Page”输出。
错误处理要比看上去更麻烦一些,尤其是在没有web.xml配置文件并且希望应用能够跨Web服务器部署时更是如此。好消息是Spring Boot将会处理这些事情!让我们看一下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;
这段配置都做了些什么呢?
定义了一个bean,即DefaultErrorAttributes,它通过特定的属性暴露了有用的错误信息,这些属性包括状态、错误码和相关的栈跟踪信息。
定义了一个BasicErrorController bean,这是一个MVC控制器,负责展现我们所看到的错误页面。
允许我们将Spring Boot的whitelabel错误页面设置为无效,这需要将配置文件application.properties中的error.whitelable.enabled设置为false。
我们还可以借助模板引擎提供自己的错误页面。例如,它的名字是error.html,ErrorTemplateMissingCondition条件会对此进行检查。
在本书后面的内容中,我们将会看到如何恰当地处理错误。
至于转码的问题,非常简单的HttpEncodingAutoConfiguration将会负责处理相关的事宜,这是通过提供Spring的CharacterEncodingFilter类来实现的。通过spring.http.encoding.charset配置,我们可以覆盖默认的编码(“UTF-8”),也可以通过spring.http.encoding.enabled禁用这项配置。
MVC自动配置原理 官网阅读 在进行项目编写前,我们还需要知道一个东西,就是SpringBoot对我们的SpringMVC还做了哪些配置,包括如何扩展,如何定制。
Spring MVC配置静态资源 你好看官,里面请!今天笔者讲的是在Spring MVC配置静态资源。不懂或者觉得我写的有问题可以在评论区留言,我看到会及时回复。 注意:本文仅用于学习参考,不可用于商业用途,如需转载请跟我联系。
【Spring MVC 系列】Spring MVC 中 Filter 配置的 6 种方式,看看你了解哪些 Filter 简介 过滤器 Filter 在 Servlet 2.3 版本中被首次提出,唯一的作用就是过滤,它不仅可以过滤请求,还可以过滤响应,当请求到达 Servlet 容器,会先经过 Filter ,然后再交给 Servlet,之后 Filter 还可以对 Servlet 的响应进一步处理。并且多个 Filter 还能形成一个链。使用图示表达如下。
Spring MVC之基于java config无xml配置的web应用构建 前一篇博文讲了SpringMVC+web.xml的方式创建web应用,用过SpringBoot的童鞋都知道,早就没有xml什么事情了,其实Spring 3+, Servlet 3+的版本,就已经支持java config,不用再写xml;本篇将介绍下,如何利用java config取代xml配置
异步社区 异步社区(www.epubit.com)是人民邮电出版社旗下IT专业图书旗舰社区,也是国内领先的IT专业图书社区,致力于优质学习内容的出版和分享,实现了纸书电子书的同步上架,于2015年8月上线运营。公众号【异步图书】,每日赠送异步新书。
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战 立即下载
相关文章
- 开涛spring3(9.4) - Spring的事务 之 9.4 声明式事务
- Prometheus + Spring Boot 应用监控
- 关于Springboot打包错误的问题 | Failed to execute goal org.springframework.boot:spring-boot-maven-plugin
- 2021最新 IDEA 启动失败 & 启动Spring boot 项目端口被占用问题 彻底解决方案
- Error creating bean with name ‘org.apache.cxf.jaxws.spring.NamespaceHandler$SpringServerFactoryBean
- Spring中的IOC和AOP
- 一句话告诉你什么是spring的ioc
- Spring验证的错误返回------BindingResult
- Spring Boot Request method DELETE not supported
- Spring MVC “404 Not Found”错误的解决
- spring aop使用,spring aop注解,Spring切面编程
- spring参数类型异常输出(二), SpringMvc参数类型转换错误输出(二)
- spring参数类型异常输出,SpringMvc参数类型转换错误输出
- Spring整合CXF步骤,Spring实现webService,spring整合WebService
- Spring MVC 学习笔记 spring mvc Schema-based configuration
- spring框架漏洞整理(Spring Cloud Config路径穿越导致的信息泄露)
- spring框架漏洞整理(Spring Framework漏洞)
- 【Spring常见错误】Rabbitmq 常见问题
- 【Spring常见错误】java.lang.NoClassDefFoundError: redis/clients/jedis/UnifiedJedis
- 【Spring常见错误】No qualifying bean of type
- Spring_错误 java.sql.SQLException: Lock wait timeout exceeded | CannotAcquireLockException 的解决
- Spring之c3p0连接池配置和使用
- spring mvc Spring Data Redis RedisTemplate [转]
- 【spring框架】JdbcTemplate
- 使用 Java 配置进行 Spring bean 管理--转
- 曹工说Spring Boot源码系列开讲了(1)-- Bean Definition到底是什么,附spring思维导图分享