
4.5 自定义错误页
4.4节向读者介绍了Spring Boot中的全局异常处理。在处理异常时,开发者可以根据实际情况返回不同的页面,但是这种异常处理方式一般用来处理应用级别的异常,有一些容器级别的错误就处理不了,例如Filter中抛出异常,使用@ControllerAdvice定义的全局异常处理机制就无法处理。因此,Spring Boot中对于异常的处理还有另外的方式,这就是本节要介绍的内容。
在Spring Boot中,默认情况下,如果用户在发起请求时发生了404错误,Spring Boot会有一个默认的页面展示给用户,如图4-10所示。

图4-10
如果发起请求时发生了500错误,Spring Boot也会有一个默认的页面展示给用户,如图4-11所示。

图4-11
事实上,Spring Boot在返回错误信息时不一定返回HTML页面,而是根据实际情况返回HTML页面或者一段JSON(若开发者发起Ajax请求,则错误信息是一段JSON)。对于开发者而言,这一段HTML或者JSON都能够自由定制。
Spring Boot中的错误默认是由BasicErrorController类来处理的,该类中的核心方法主要有两个:

其中,errorHtml方法用来返回错误HTML页面,error用来返回错误JSON,具体返回的是HTML还是JSON,则要看请求头的Accept参数。返回JSON的逻辑很简单,不必过多介绍,返回HTML的逻辑稍微有些复杂,在errorHtml方法中,通过调用resolveErrorView方法来获取一个错误视图的ModelAndView。而resolveErrorView方法的调用最终会来到DefaultErrorViewResolver类中。
DefaultErrorViewResolver类是Spring Boot中默认的错误信息视图解析器,部分源码如下:

从这一段源码中可以看到,Spring Boot默认是在error目录下查找4xx、5xx的文件作为错误视图,当找不到时会回到errorHtml方法中,然后使用error作为默认的错误页面视图名,如果名为error的视图也找不到,用户就会看到本节一开始展示的两个错误提示页面。整个错误处理流程大致就是这样的。
4.5.1 简单配置
通过上面的介绍,读者可能已经发现,要自定义错误页面其实很简单,提供4xx和5xx页面即可。如果开发者不需要向用户展示详细的错误信息,那么可以把错误信息定义成静态页面,直接在resources/static目录下创建error目录,然后在error目录中创建错误展示页面。错误展示页面的命名规则有两种:一种是4xx.html、5xx.html;另一种是直接使用响应码命名文件,例如404.html、405.html、500.html。第二种命名方式划分得更细,当出错时,不同的错误会展示不同的错误页面,如图4-12所示。

图4-12
此时,当用户访问一个不存在的路径时,就会展示404.html页面中的内容,如图4-13所示。

图4-13
修改Controller,提供一个会抛异常的请求,代码如下:

访问该接口,就会展示500.html中的内容,如图4-14所示。

图4-14
这种定义都是使用了静态HTML页面,无法向用户展示完整的错误信息,若采用视图模板技术,则可以向用户展示更多的错误信息。如果要使用HTML模板,那么先引入模板相关的依赖,这里以Thymeleaf为例,Thymeleaf页面模板默认处于classpath:/templates/目录下,因此在该目录下先创建error目录,再创建错误展示页,如图4-15所示。

图4-15
由于模板页面展示信息比较灵活,因此可以直接创建4xx.html、5xx.html。以4xx.html页面为例,其内容如下:

Spring Boot在这里一共返回了5条错误相关的信息,分别是timestamp、status、error、message以及path。5xx.html页面的内容与4xx.html页面的内容一致。
此时,用户访问一个不存在的地址,4xx.html页面中的内容将被展示出来,如图4-16所示。

图4-16
若用户访问一个会抛异常的地址,例如上文的/hello接口,则会展示5xx.html页面的内容,如图4-17所示。

图4-17
注意
若用户定义了多个错误页面,则响应码.html页面的优先级高于4xx.html、5xx.html页面的优先级,即若当前是一个404错误,则优先展示404.html而不是4xx.html;动态页面的优先级高于静态页面,即若resources/templates和resources/static下同时定义了4xx.html,则优先展示resources/templates/4xx.html。
4.5.2 复杂配置
上面这种配置还是不够灵活,只能定义HTML页面,无法处理JSON的定制。Spring Boot中支持对Error信息的深度定制,接下来将从三个方面介绍深度定制:自定义Error数据、自定义Error视图以及完全自定义。
1. 自定义Error数据
自定义Error数据就是对返回的数据进行自定义。经过4.5.1小节的介绍,读者已经了解到Spring Boot返回的Error信息一共有5条,分别是timestamp、status、error、message以及path。在BasicErrorController的errorHtml方法和error方法中,都是通过getErrorAttributes方法获取Error信息的。该方法最终会调用到DefaultErrorAttributes类的getErrorAttributes方法,而DefaultErrorAttributes类是在ErrorMvcAutoConfiguration中默认提供的。ErrorMvcAutoConfiguration类的errorAttributes方法源码如下:

从这段源码中可以看出,当系统没有提供ErrorAttributes时才会采用DefaultErrorAttributes。因此自定义错误提示时,只需要自己提供一个ErrorAttributes即可,而DefaultErrorAttributes是ErrorAttributes的子类,因此只需要继承DefaultErrorAttributes即可,代码如下:

代码解释:
• 自定义MyErrorAttribute继承自DefaultErrorAttributes,重写DefaultErrorAttributes中的getErrorAttributes方法。MyErrorAttribute类添加@Component注解,该类将被注册到Spring容器中。
• 第6、7行通过super.getErrorAttributes获取Spring Boot默认提供的错误信息,然后在此基础上添加Error信息或者移除Error信息。
此时,当系统抛出异常时,错误信息将被修改,以4.5.1节中的动态页面模板404.html为例,修改404.html,代码如下:

在第9~12行添加了custommsg属性,此时访问一个不存在的路径,就能看到自定义的Error信息,并且可以看到默认的error被移除了,如图4-18所示。

图4-18
如果通过Postman等工具来发起这个请求,那么返回的JSON数据中也是如此,如图4-19所示。

图4-19
2. 自定义Error视图
Error视图是展示给用户的页面,在BasicErrorController的errorHtml方法中调用resolveErrorView方法获取一个ModelAndView实例。resolveErrorView方法是由ErrorViewResolver提供的,通过ErrorMvcAutoConfiguration类的源码可以看到Spring Boot默认采用的ErrorViewResolver是DefaultErrorViewResolver。ErrorMvcAutoConfiguration部分源码如下:

从这一段源码可以看到,如果用户没有定义ErrorViewResolver,那么默认使用的ErrorViewResolver是DefaultErrorViewResolver,正是在DefaultErrorViewResolver中配置了默认去error目录下寻找4xx.html、5xx.html。因此,开发者想要自定义Error视图,只需要提供自己的ErrorViewResolver即可,代码如下:

代码解释:
• 自定义MyErrorViewResolver实现ErrorViewResolver接口并实现接口中的resolveErrorView方法,使用@Component注解将该类注册到Spring容器中。
• 在resolveErrorView方法中,最后一个Map参数就是Spring Boot提供的默认的5条Error信息(可以按照前面自定义Error数据的步骤对这5条消息进行修改)。在resolveErrorView方法中,返回一个ModelAndView,在ModelAndView中设置Error视图和Error数据。
• 理论上,开发者也可以通过实现ErrorViewResolver接口来实现Error数据的自定义,但是如果只是单纯地想自定义Error数据,还是建议继承DefaultErrorAttributes。
接下来在resources/templates目录下提供errorPage.html视图,内容如下:

在errorPage.html中,除了展示Spring Boot提供的5条Error信息外,也展示了开发者自定义的Error信息。此时,无论请求发生4xx的错误(如图4-20所示)还是发生5xx的错误(如图4-21所示),都会来到errorPage.html页面。

图4-20

图4-21
3. 完全自定义
前面提到的两种自定义方式都是对BasicErrorController类中的某个环节进行修补。查看Error自动化配置类ErrorMvcAutoConfiguration,读者可以发现BasicErrorController本身只是一个默认的配置,相关源码如下:

从这段源码中可以看到,若开发者没有提供自己的ErrorController,则Spring Boot提供BasicErrorController作为默认的ErrorController。因此,如果开发者需要更加灵活地对Error视图和数据进行处理,那么只需要提供自己的ErrorController即可。提供自己的ErrorController有两种方式:一种是实现ErrorController接口,另一种是直接继承BasicErrorController。由于ErrorController接口只提供一个待实现的方法,而BasicErrorController已经实现了很多功能,因此这里选择第二种方式,即通过继承BasicErrorController来实现自己的ErrorController。具体定义如下:

代码解释:
• 自定义MyErrorController继承自BasicErrorController并添加@Controller注解,将MyErrorController注册到Spring MVC容器中。
• 由于BasicErrorController没有无参构造方法,因此在创建BasicErrorController实例时需要传递参数,在MyErrorController的构造方法上添加@Autowired注解注入所需参数。
• 参考BasicErrorController中的实现,重写errorHtml和error方法,对Error的视图和数据进行充分的自定义。
最后,在resources/templates目录下提供myErrorPage.html页面作为视图页面,代码如下:

访问一个不存在的页面,就可以看到自定义的错误提示了,如图4-22所示。

图4-22
如果通过Postman等工具发起这个请求,那么返回数据为一段JSON,如图4-23所示。

图4-23
Spring Boot中对异常的处理还是非常容易的,Spring Boot虽然提供了非常丰富的自动化配置方案,但是也允许开发者根据实际情况进行完全的自定义,开发者在使用过程中可以结合具体情况选择合适的Error处理方案。