SpringBoot的全局异常处理

SpringBoot的全局异常处理

今天讲解一下如何在SpringBoot实现全局异常机制,在没有用springboot大家要实现这一的功能基本上都是通过aop的思想,还是有点麻烦,而现在springboot中对它要进行了一次封装,开发者使用起来更加的简单,接下先通过代码演示效果,然后再分析一下原理,好了废话不多说直接上代码,看代码结构:

看一下对应的Pom.xml文件内容:


 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0 </modelVersion>
  6. <artifactId>spring_boot_exception </artifactId>
  7. <parent>
  8. <groupId>org.springframework.boot </groupId>
  9. <artifactId>spring-boot-starter-parent </artifactId>
  10. <version>1.5.2.RELEASE </version>
  11. </parent>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  14. <project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding>
  15. <java.version>1.8 </java.version>
  16. </properties>
  17. <dependencies>
  18. <!-- -->
  19. <dependency>
  20. <groupId>org.projectlombok </groupId>
  21. <artifactId>lombok </artifactId>
  22. <version>1.16.18 </version>
  23. </dependency>
  24. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  25. <dependency>
  26. <groupId>com.alibaba </groupId>
  27. <artifactId>fastjson </artifactId>
  28. <version>1.2.44 </version>
  29. </dependency>
  30. <!--支持web开发-->
  31. <dependency>
  32. <groupId>org.springframework.boot </groupId>
  33. <artifactId>spring-boot-starter-web </artifactId>
  34. </dependency>
  35. </dependencies>
  36. </project>

定义一个封装json的工具类,使用阿里巴巴的fastjson:


 
 
  1. package com.springboot.exception.json;
  2. import com.alibaba.fastjson.JSON;
  3. import lombok.Data;
  4. import java.io.Serializable;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. /**
  8. * lombok格式化数据
  9. */
  10. @Data
  11. public class JsonResult implements Serializable{
  12. private int code; //返回码 非0即失败
  13. private String msg; //消息提示
  14. private Map<String, Object> data; //返回的数据
  15. public JsonResult(){};
  16. public JsonResult(int code, String msg, Map<String, Object> data) {
  17. this.code = code;
  18. this.msg = msg;
  19. this.data = data;
  20. }
  21. public static String success() {
  22. return success( new HashMap( 0));
  23. }
  24. public static String success(Map<String, Object> data) {
  25. return JSON.toJSONString( new JsonResult( 0, "解析成功", data));
  26. }
  27. public static String failed() {
  28. return failed( "解析失败");
  29. }
  30. public static String failed(String msg) {
  31. return failed(- 1, msg);
  32. }
  33. public static String failed(int code, String msg) {
  34. return JSON.toJSONString( new JsonResult(code, msg, new HashMap( 0)));
  35. }
  36. }

接下来定义一个全局异常处理类:


 
 
  1. package com.springboot.exception.global_excep;
  2. import com.springboot.exception.json.JsonResult;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.ConversionNotSupportedException;
  6. import org.springframework.beans.TypeMismatchException;
  7. import org.springframework.http.converter.HttpMessageNotReadableException;
  8. import org.springframework.http.converter.HttpMessageNotWritableException;
  9. import org.springframework.web.HttpMediaTypeNotAcceptableException;
  10. import org.springframework.web.HttpRequestMethodNotSupportedException;
  11. import org.springframework.web.bind.MissingServletRequestParameterException;
  12. import org.springframework.web.bind.annotation.ControllerAdvice;
  13. import org.springframework.web.bind.annotation.ExceptionHandler;
  14. import org.springframework.web.bind.annotation.ResponseBody;
  15. import java.io.IOException;
  16. /**
  17. * @Author 18011618
  18. * @Description 全局异常拦截器
  19. * @Date 16:38 2018/7/5
  20. * @Modify By
  21. */
  22. @ControllerAdvice
  23. @ResponseBody
  24. public class GlobalExceptionHandler {
  25. private static final String logExceptionFormat = "Capture Exception By GlobalExceptionHandler: Code: %s Detail: %s";
  26. private static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
  27. //运行时异常
  28. @ExceptionHandler(RuntimeException.class)
  29. public String runtimeExceptionHandler(RuntimeException ex) {
  30. return resultFormat( 1, ex);
  31. }
  32. //空指针异常
  33. @ExceptionHandler(NullPointerException.class)
  34. public String nullPointerExceptionHandler(NullPointerException ex) {
  35. System.err.println( "NullPointerException:");
  36. return resultFormat( 2, ex);
  37. }
  38. //类型转换异常
  39. @ExceptionHandler(ClassCastException.class)
  40. public String classCastExceptionHandler(ClassCastException ex) {
  41. return resultFormat( 3, ex);
  42. }
  43. //IO异常
  44. @ExceptionHandler(IOException.class)
  45. public String iOExceptionHandler(IOException ex) {
  46. return resultFormat( 4, ex);
  47. }
  48. //未知方法异常
  49. @ExceptionHandler(NoSuchMethodException.class)
  50. public String noSuchMethodExceptionHandler(NoSuchMethodException ex) {
  51. return resultFormat( 5, ex);
  52. }
  53. //数组越界异常
  54. @ExceptionHandler(IndexOutOfBoundsException.class)
  55. public String indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
  56. return resultFormat( 6, ex);
  57. }
  58. //400错误
  59. @ExceptionHandler({HttpMessageNotReadableException.class})
  60. public String requestNotReadable(HttpMessageNotReadableException ex) {
  61. System.out.println( "400..requestNotReadable");
  62. return resultFormat( 7, ex);
  63. }
  64. //400错误
  65. @ExceptionHandler({TypeMismatchException.class})
  66. public String requestTypeMismatch(TypeMismatchException ex) {
  67. System.out.println( "400..TypeMismatchException");
  68. return resultFormat( 8, ex);
  69. }
  70. //400错误
  71. @ExceptionHandler({MissingServletRequestParameterException.class})
  72. public String requestMissingServletRequest(MissingServletRequestParameterException ex) {
  73. System.out.println( "400..MissingServletRequest");
  74. return resultFormat( 9, ex);
  75. }
  76. //405错误
  77. @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
  78. public String request405(HttpRequestMethodNotSupportedException ex) {
  79. return resultFormat( 10, ex);
  80. }
  81. //406错误
  82. @ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
  83. public String request406(HttpMediaTypeNotAcceptableException ex) {
  84. System.out.println( "406...");
  85. return resultFormat( 11, ex);
  86. }
  87. //500错误
  88. @ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
  89. public String server500(RuntimeException ex) {
  90. System.out.println( "500...");
  91. return resultFormat( 12, ex);
  92. }
  93. //栈溢出
  94. @ExceptionHandler({StackOverflowError.class})
  95. public String requestStackOverflow(StackOverflowError ex) {
  96. return resultFormat( 13, ex);
  97. }
  98. //除数不能为0
  99. @ExceptionHandler({ArithmeticException.class})
  100. public String arithmeticException(ArithmeticException ex) {
  101. return resultFormat( 13, ex);
  102. }
  103. //其他错误
  104. @ExceptionHandler({Exception.class})
  105. public String exception(Exception ex) {
  106. return resultFormat( 14, ex);
  107. }
  108. private <T extends Throwable> String resultFormat(Integer code, T ex) {
  109. ex.printStackTrace();
  110. log.error(String.format(logExceptionFormat, code, ex.getMessage()));
  111. return JsonResult.failed(code, ex.getMessage());
  112. }
  113. }

代码写完了,讲解一下几个新的注解:

@ControllerAdvice: 可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute,并应用到所有@RequestMapping中

@ExceptionHandler:标识异常类型对应的处理方法

比如下面这个方法:就是处理当遇到了空指针异常对应的处理方法

@ExceptionHandler(NullPointerException.class)

public String nullPointerExceptionHandler(NullPointerException ex) {

    System.err.println("NullPointerException:");

    return resultFormat(2, ex);

}

接下来写一个测试的controller:


 
 
  1. package com.springboot.exception.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * @Author 18011618
  6. * @Date 15:59 2018/7/13
  7. * @Function 测试异常的controller
  8. */
  9. @RestController
  10. public class ExceptionController {
  11. @RequestMapping( "/exce")
  12. public String showInfo(){
  13. System.err.println( "dddddddddddddd");
  14. String info = "你好";
  15. int a = 1/ 0;
  16. return info;
  17. }
  18. }

最看看一下对应的application.yaml配置文件


 
 
  1. server:
  2. port: 8888

最后写一个启动应用类:


 
 
  1. package com.springboot.exception;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * @Author 18011618
  6. * @Description
  7. * @Date 16:00 2018/7/13
  8. * @Modify By
  9. */
  10. @SpringBootApplication
  11. public class ExceptionApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(ExceptionApplication.class,args);
  14. }
  15. }

这个时候启动应用类之后,然后访问浏览器 http://localhost:8888/exce 对应的效果如下

这个返回的消息就是全局异常帮忙处理的。其它异常也可以去测试 都是可以拦截的,这里要注意一下:

如果不想用@ResponseBody的话,可以使用@RestControllerAdvice代替@ControllerAdvice

OK 效果实现了,它主要是在Spring mvc 启动时调用RequestMappingHandlerAdapter类的initControllerAdviceCache()方法进行初始化

到此为止,全局异常处理就讲解完了

版权声明:转载请标明博客地址,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值