10.10、spring boot的web应用——定义错误页面(2)——通过实现ErrorPageRegistrar根据不同的错误类型显示网页

一、spring boot默认显示的错误

下面首先演示下没有自定义显示错误信息的默认网页,在spring boot中已经定义好了。
1、首先定义controller层,用于响应请求

package com.lzj.spring.boot.config;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    /*一、把错误抛在页面上
     * 1.错误异常自定义在org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration.class中。*/
    @RequestMapping("/erro")
    /*抛出参数不合法的异常到页面*/
    public String erro(){
        throw new IllegalArgumentException("arg is empty");
    }
}

2、spring boot的启动类为:

@SpringBootApplication(scanBasePackages="com.lzj.spring.boot")
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

运行启动类,在浏览器中发送http://localhost:8080/erro请求,由erro()方法响应请求,由于erro()方法抛出异常,并把异常信息返回到请求页面。如下所示,即为spring boot显示异常信息的默认页面。
这里写图片描述
上面显示错误信息的网页就是spring boot中默认显示的。默认显示的网页已在spring-boot-autoconfigure-1.5.9.RELEASE.jar中的org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration.class中定义了,源码如下:

        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>");

二、tomcat默认显示的错误信息

由于spring boot的默认显示的错误信息已经在ErrorMvcAutoConfiguration类中进行定义了,如果想以tomcat的默认显示错误信息的网页显示错误信息,只需要在容器中扫描时,排除ErrorMvcAutoConfiguration类的bean就可以了。在启动类中定义如下:

/*排除扫描ErrorMvcAutoConfiguration类型的bean*/
@SpringBootApplication(exclude=ErrorMvcAutoConfiguration.class, scanBasePackages="com.lzj.spring.boot")
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

然后在启动启动类,并在浏览器中发送http://localhost:8080/erro请求,浏览器此时显示tomcat的默认显示错误的网页。
tomcat的默认错误信息显示

三、通过实现ErrorPageRegistrar来注册不同错误类型显示不同的网页

Controller层和启动类不变,下面定义一个配置根据不同错误显示不同网页的配置类。配置类要实现ErrorPageRegistrar类,并且要注入到容器中。

package com.lzj.spring.boot.error;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.ErrorPageRegistrar;
import org.springframework.boot.web.servlet.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

@Component
public class MyError implements ErrorPageRegistrar {

    @Override
    public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
        /*1、按错误的类型显示错误的网页*/
        /*错误类型为404,找不到网页的,默认显示404.html网页*/
        ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
        /*错误类型为500,表示服务器响应错误,默认显示500.html网页*/
        ErrorPage e500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
        errorPageRegistry.addErrorPages(e404, e500);
    }

}

在/static/目录下分别创建404.html和500.html网页
404.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h1>This is 404 erro page</h1>

</body>
</html>

500.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h1>This is 500 erro page</h1>

</body>
</html>

启动spring boot工程,在浏览器中发送http://localhost:8080/erro请求,由erro()响应请求,并抛出异常,表示服务器端响应错误,浏览器默认显示505.html,如下所示:
这里写图片描述
在浏览器中发送http://localhost:8080/erro2请求,controller层没有响应请求的方法,浏览器默认显示:
这里写图片描述

上面例子是根据不同的请求响应错误类型显示不同的网页,也可以根据异常类型来显示错误网页。如下所示:

@Component
public class MyError implements ErrorPageRegistrar {

    @Override
    public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
        /*1、按错误的类型显示错误的网页*/
        ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
        ErrorPage e500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
        /*2、按具体某个异常显示错误的网页*/
        /*当某个异常即可以根据错误类型显示错误网页,由可以根据某个具体的异常来显示错误网页时,优先根据具体的某个异常显示错误的网页*/
        ErrorPage argsException = new ErrorPage(IllegalArgumentException.class, "/args.html");
        errorPageRegistry.addErrorPages(e404, e500, argsException);
    }

}

在/static/目录下创建args.html网页
args.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h1>Arg is empty</h1>

</body>
</html>

启动spring boot工程,在浏览器中发送http://localhost:8080/erro请求,由erro()响应请求,并抛出异常,浏览器默认显示args.html网页。
这里写图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值