博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《精通Spring MVC 4》——1.7 错误与转码配置
阅读量:7066 次
发布时间:2019-06-28

本文共 3333 字,大约阅读时间需要 11 分钟。

本节书摘来自异步社区《精通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 thatthe error View is// available@AutoConfigureBefore(WebMvcAutoConfiguration.class)@Configurationpublic class ErrorMvcAutoConfiguration implementsEmbeddedServletContainerCustomizer,        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(ErrorAttributeserrorAttributes) {        return new BasicErrorController(errorAttributes);    }    @Override    public void customize(ConfigurableEmbeddedServletContainercontainer) {        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(                "

Whitelabel Error Page

" + "

This application has no explicit mappingfor /error, so you are seeing this as a fallback.

" + "
${timestamp}
" + "
There was an unexpected error(type=${error}, status=${status}).
" + "
${message}
"); @Bean(name = "error") @ConditionalOnMissingBean(name = "error") public View defaultErrorView() { return this.defaultErrorView; } // If the user adds @EnableWebMvc then the bean name viewresolver from // WebMvcAutoConfiguration disappears, so add it back in toavoid disappointment. @Bean @ConditionalOnMissingBean(BeanNameViewResolver.class) public BeanNameViewResolver beanNameViewResolver() { BeanNameViewResolver resolver = newBeanNameViewResolver(); 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禁用这项配置。

转载地址:http://qhall.baihongyu.com/

你可能感兴趣的文章
docker-swarm建立本地集成开发环境
查看>>
MySQL下全文索引
查看>>
SQL Server 作业同步
查看>>
10 个最酷的 Linux 单行命令 — LinuxTOY
查看>>
left join
查看>>
一起谈.NET技术,Microsoft Ribbon for WPF 正式发布
查看>>
android如何拍照以及返回拍的图片(经过验证的实际例子)
查看>>
【编译】UI设计师必读的13篇文章
查看>>
隐私保护利器-----开辟密盘
查看>>
Silverlight调用JS获得浏览器窗口大小
查看>>
android中文乱码解决大全
查看>>
Getting Started with iOS Development Part10:Customizing your Mobile target's Splash screen
查看>>
8个很棒的 jQuery 倒计时插件和教程
查看>>
python implemention javascript encodeURIComponent
查看>>
SQL查询语句精华
查看>>
Socket服务器整体架构概述
查看>>
Sketchup二次开发API之UI类
查看>>
串连接
查看>>
jsp连接数据库的乱码问题 servlet请求参数编码处理get post
查看>>
Java小项目--坦克大战(version1.0)
查看>>