Spring Boot Web Error Page处理
spring Boot默认是whitelabel error page. 其实我们可以自己处理,由于时间有限,所以就简单说明一下方法。
首先配置
@Configuration
public class ErrorPageConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
public void customize(ConfigurableEmbeddedServletContainer container) {
ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/400.html");
ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404/");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
container.addErrorPages(error400Page, error401Page, error404Page, error500Page);
}
};
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
细心的朋友会看到,404不是html, 这儿为了掩饰,所以用了两种方法,如果是html的方法,需要将html文件放到resources/static目录下。404处理方式,就需要我们自己处理/404请求,与一般的Controller中处理Request类似。如下:
@RequestMapping("404")
public String error404() {
return "error404";
}
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
用到了模版,所以需要在resources/templates目录下创建error404.html文件
其实配置的时候,也可以用继承的方式:
@Configuration
public class ErrorPageConfig implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.addErrorPages(
new ErrorPage(HttpStatus.BAD_REQUEST, "/4O0.html"),
new ErrorPage(HttpStatus.UNAUTHORIZED, "/4O1.html"),
new ErrorPage(HttpStatus.NOT_FOUND, "/404/"),
new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html")
);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
关于异常的处理可以参看:http://blog.didispace.com/springbootexception/
Spring Boot Web Error Page处理的更多相关文章
- Spring Boot : Whitelabel Error Page解决方案
楼主最近爱上了一个新框架--Spring Boot, 搭建快还不用写一堆xml,最重要的是自带Tomcat 真是好 pom.xml <?xml version="1.0" e ...
- spring boot: Whitelabel Error Page的解决方案
http://blog.csdn.net/u014788227/article/details/53670112
- PART 5: INTEGRATING SPRING SECURITY WITH SPRING BOOT WEB
转自:http://justinrodenbostel.com/2014/05/30/part-5-integrating-spring-security-with-spring-boot-web/ ...
- Springboot 系列(七)Spring Boot web 开发之异常错误处理机制剖析
前言 相信大家在刚开始体验 Springboot 的时候一定会经常碰到这个页面,也就是访问一个不存在的页面的默认返回页面. 如果是其他客户端请求,如接口测试工具,会默认返回JSON数据. { &quo ...
- 转-spring boot web相关配置
spring boot web相关配置 80436 spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何w ...
- Spring Boot Web Executable Demo
Spring Boot Web Executable Demo */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.sr ...
- Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件
1. 拦截器 Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分.并没有本质的不同,都是通过实现 HandlerInterceptor ...
- Springboot 系列(五)Spring Boot web 开发之静态资源和模版引擎
前言 Spring Boot 天生的适合 web 应用开发,它可以快速的嵌入 Tomcat, Jetty 或 Netty 用于包含一个 HTTP 服务器.且开发十分简单,只需要引入 web 开发所需的 ...
- Spring boot 处理 error 的套路
Spring boot 处理 error 的基本流程: Controller -> 发生错误 -> BasicErrorController -> 根据 @RequestMappin ...
随机推荐
- golang学习之struct
结构体定义的一般方式如下: type identifier struct { field1 type1 field2 type2 ... } type T struct {a, b int} 也是合法 ...
- [android] 天气app布局练习
主要练习一下RelativeLayout和LinearLayout <RelativeLayout xmlns:android="http://schemas.android.com/ ...
- JS原型链继承
继承普通版 继承逻辑上都差不多,普通版调用方式比较繁琐,不利于反复大量的使用: (function (){ //创建一个人员类 function Person(name){ this.name = n ...
- Mybatis学习随笔
学习Mybatis路径(适合有java基础和mysql基础的小伙伴) 1.把项目搭建起来,跑一跑感受一下 2.测试基本映射 3.测试高级映射 4.测试动态sql 5.学习懒加载与缓存 6.与sprin ...
- 扩展运算符&reset运算符
扩展运算符用三个点号表示,功能是把数组或类数组对象展开成一系列用逗号隔开的值 var foo = function(a, b, c) { console.log(a); console.log(b); ...
- idea output 消失找不到
今天一不小心把idea的 debug的控制台output 搞丢了, 半天找不到,原来是在这里隐藏着 Restore layout
- Linux-vi/vim编辑器常用命令与用法
vi/vim是什么? Linux世界几乎所有的配置文件都是以纯文本形式存在的,而在所有的Linux发行版系统上都有vi编辑器,因此利用简单的文字编辑软件就能够轻松地修改系统的各种配置了,非常方便.vi ...
- Less的guards and argument matching
less guards/argument matching: .setbackground(@number) when (@number>0){ .setbackground( @number ...
- MacOS上好用的软件
持续补充中…… 记录一些除了MacOS(Sierra)自带工具外,其他好用的软件. 截屏软件 Jietu——在AppStore中查找“Jietu”就可以找到这款软件.这是腾讯出品的一款用于MacOS系 ...
- lua之m进制转换为n进制-任意进制转换算法
够无聊的写这个,为防止需要的人也无聊一遍,写个吧 算法有n种,但是,咱们一种就够用了 --数组倒序排列 local function orderByDesc( input ) local output ...