一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

  • DB系列

  • 搜索系列

  • MQ系列

  • WEB系列

    • Request

    • Response

      • 【WEB系列】Freemaker环境搭建
      • 【WEB系列】Thymeleaf环境搭建
      • 【WEB系列】Beetl环境搭建
      • 【WEB系列】返回文本、网页、图片的操作姿势
      • 【WEB系列】请求重定向
      • 【WEB系列】404、500异常页面配置
      • 【WEB系列】全局异常处理
      • 【WEB系列】自定义异常处理HandlerExceptionResolver
        • I. 环境搭建
        • II. HandlerExceptionResolver
          • 1. 自定义异常处理
          • 2. 测试case
          • 3. 小结
        • II. 其他
          • 1. 一灰灰Blog
      • 【WEB系列】开启GZIP数据压缩
      • 【WEB系列】RestTemplate 4xx/5xx 异常信息捕获
      • 【WEB系列】自定义返回Http Code的n种姿势
      • 【WEB系列】异步请求知识点与使用姿势小结
      • 【WEB系列】SSE服务器发送事件详解
      • 【WEB系列】thymeleaf foreach踩坑记录
      • 【WEB系列】如何支持下划线驼峰互转的传参与返回
    • RestTemplate

    • WebClient

    • WebFlux

    • WebSocket

    • Web三剑客

    • 实例

    • 其他

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • WEB系列
  • Response
一灰灰
2019-10-12

【WEB系列】自定义异常处理HandlerExceptionResolver

关于Web应用的全局异常处理,上一篇介绍了ControllerAdvice结合@ExceptionHandler的方式来实现web应用的全局异常管理;

本篇博文则带来另外一种并不常见的使用方式,通过实现自定义的HandlerExceptionResolver,来处理异常状态

上篇博文链接: SpringBoot系列教程web篇之全局异常处理 (opens new window)

# I. 环境搭建

首先得搭建一个web应用才有可能继续后续的测试,借助SpringBoot搭建一个web应用属于比较简单的活;

创建一个maven项目,pom文件如下

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7</version>
    <relativePath/> <!-- lookup parent from update -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.45</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

# II. HandlerExceptionResolver

# 1. 自定义异常处理

HandlerExceptionResolver顾名思义,就是处理异常的类,接口就一个方法,出现异常之后的回调,四个参数中还携带了异常堆栈信息

@Nullable
ModelAndView resolveException(
		HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex);
1
2
3

我们自定义异常处理类就比较简单了,实现上面的接口,然后将完整的堆栈返回给调用方

public class SelfExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        String msg = GlobalExceptionHandler.getThrowableStackInfo(ex);

        try {
            response.addHeader("Content-Type", "text/html; charset=UTF-8");
            response.getWriter().append("自定义异常处理!!! \n").append(msg).flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

// 堆栈信息打印方法如下
public static String getThrowableStackInfo(Throwable e) {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    e.printStackTrace(new java.io.PrintWriter(buf, true));
    String msg = buf.toString();
    try {
        buf.close();
    } catch (Exception t) {
        return e.getMessage();
    }
    return msg;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

仔细观察上面的代码实现,有下面几个点需要注意

  • 为了确保中文不会乱码,我们设置了返回头 response.addHeader("Content-Type", "text/html; charset=UTF-8"); 如果没有这一行,会出现中文乱码的情况
  • 我们纯后端应用,不想返回视图,直接想Response的输出流中写入数据返回 response.getWriter().append("自定义异常处理!!! \n").append(msg).flush();; 如果项目中有自定义的错误页面,可以通过返回ModelAndView来确定最终返回的错误页面
  • 上面一个代码并不会直接生效,需要注册,可以在WebMvcConfigurer的子类中实现注册,实例如下
@SpringBootApplication
public class Application implements WebMvcConfigurer {
    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        resolvers.add(0, new SelfExceptionHandler());
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}
1
2
3
4
5
6
7
8
9
10
11

# 2. 测试case

我们依然使用上篇博文的用例来测试

@Controller
@RequestMapping(path = "page")
public class ErrorPageRest {

    @ResponseBody
    @GetMapping(path = "divide")
    public int divide(int sub) {
        return 1000 / sub;
    }
}
1
2
3
4
5
6
7
8
9
10

下面分别是404异常和500异常的实测情况

500异常会进入我们的自定义异常处理类, 而404依然走的是默认的错误页面,所以如果我们需要捕获404异常,依然需要在配置文件中添加

# 出现错误时, 直接抛出异常
spring.mvc.throw-exception-if-no-handler-found=true
# 设置静态资源映射访问路径
spring.mvc.static-path-pattern=/statics/**
# spring.resources.add-mappings=false
1
2
3
4
5

为什么404需要额外处理?

下面尽量以通俗易懂的方式说明下这个问题

  • java web应用,除了返回json类数据之外还可能返回网页,js,css
  • 我们通过 @ResponseBody来表明一个url返回的是json数据(通常情况下是这样的,不考虑自定义实现)
  • 我们的@Controller中通过@RequestMapping定义的REST服务,返回的是静态资源
  • 那么js,css,图片这些文件呢,在我们的web应用中并不会定义一个REST服务
  • 所以当接收一个http请求,找不到url关联映射时,默认场景下不认为这是一个NoHandlerFoundException,不抛异常,而是到静态资源中去找了(静态资源中也没有,为啥不抛NoHandlerFoundException呢?这个异常表示这个url请求没有对应的处理器,但是我们这里呢,给它分配到了静态资源处理器了ResourceHttpRequestHandler)

针对上面这点,如果有兴趣深挖的同学,这里给出关键代码位置

// 进入方法: `org.springframework.web.servlet.DispatcherServlet#doDispatch`

// debug 节点
Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
	noHandlerFound(processedRequest, response);
	return;
}

// 核心逻辑
// org.springframework.web.servlet.DispatcherServlet#getHandler
@Nullable
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
	if (this.handlerMappings != null) {
		for (HandlerMapping hm : this.handlerMappings) {
			if (logger.isTraceEnabled()) {
				logger.trace(
						"Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
			}
			HandlerExecutionChain handler = hm.getHandler(request);
			if (handler != null) {
				return handler;
			}
		}
	}
	return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 3. 小结

本篇博文虽然也介绍了一种新的全局异常处理方式,实现效果和ControllerAdvice也差不多,但是并不推荐用这种方法, 原因如下

  • HandlerExceptionResolver的方式没有ControllerAdvice方式简介优雅
  • 官方提供的DefaultHandlerExceptionResolver已经非常强大了,基本上覆盖了http的各种状态码,我们自己再去定制的必要性不大

# II. 其他

# web系列博文

  • 191010-SpringBoot系列教程web篇之全局异常处理 (opens new window)
  • 190930-SpringBoot系列教程web篇之404、500异常页面配置 (opens new window)
  • 190929-SpringBoot系列教程web篇之重定向 (opens new window)
  • 190913-SpringBoot系列教程web篇之返回文本、网页、图片的操作姿势 (opens new window)
  • 190905-SpringBoot系列教程web篇之中文乱码问题解决 (opens new window)
  • 190831-SpringBoot系列教程web篇之如何自定义参数解析器 (opens new window)
  • 190828-SpringBoot系列教程web篇之Post请求参数解析姿势汇总 (opens new window)
  • 190824-SpringBoot系列教程web篇之Get请求参数解析姿势汇总 (opens new window)
  • 190822-SpringBoot系列教程web篇之Beetl环境搭建 (opens new window)
  • 190820-SpringBoot系列教程web篇之Thymeleaf环境搭建 (opens new window)
  • 190816-SpringBoot系列教程web篇之Freemaker环境搭建 (opens new window)
  • 190421-SpringBoot高级篇WEB之websocket的使用说明 (opens new window)
  • 190327-Spring-RestTemplate之urlencode参数解析异常全程分析 (opens new window)
  • 190317-Spring MVC之基于java config无xml配置的web应用构建 (opens new window)
  • 190316-Spring MVC之基于xml配置的web应用构建 (opens new window)
  • 190213-SpringBoot文件上传异常之提示The temporary upload location xxx is not valid (opens new window)

# 项目源码

  • 工程:https://github.com/liuyueyi/spring-boot-demo (opens new window)
  • 项目:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/209-web-error (opens new window)

# 1. 一灰灰Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰blog

编辑 (opens new window)
#Web#异常处理
上次更新: 2021/10/15, 19:56:22
【WEB系列】全局异常处理
【WEB系列】开启GZIP数据压缩

← 【WEB系列】全局异常处理 【WEB系列】开启GZIP数据压缩→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×