SpringBoot的全局异常处理
今天讲解一下如何在SpringBoot实现全局异常机制,在没有用springboot大家要实现这一的功能基本上都是通过aop的思想,还是有点麻烦,而现在springboot中对它要进行了一次封装,开发者使用起来更加的简单,接下先通过代码演示效果,然后再分析一下原理,好了废话不多说直接上代码,看代码结构:
看一下对应的Pom.xml文件内容:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<project xmlns="http://maven.apache.org/POM/4.0.0"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-
<modelVersion>4.0.0
</modelVersion>
-
-
<artifactId>spring_boot_exception
</artifactId>
-
<parent>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-parent
</artifactId>
-
<version>1.5.2.RELEASE
</version>
-
</parent>
-
-
<properties>
-
<project.build.sourceEncoding>UTF-8
</project.build.sourceEncoding>
-
<project.reporting.outputEncoding>UTF-8
</project.reporting.outputEncoding>
-
<java.version>1.8
</java.version>
-
</properties>
-
-
-
<dependencies>
-
<!-- -->
-
<dependency>
-
<groupId>org.projectlombok
</groupId>
-
<artifactId>lombok
</artifactId>
-
<version>1.16.18
</version>
-
</dependency>
-
-
-
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
-
<dependency>
-
<groupId>com.alibaba
</groupId>
-
<artifactId>fastjson
</artifactId>
-
<version>1.2.44
</version>
-
</dependency>
-
-
<!--支持web开发-->
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-web
</artifactId>
-
</dependency>
-
-
</dependencies>
-
</project>
定义一个封装json的工具类,使用阿里巴巴的fastjson:
-
package com.springboot.exception.json;
-
-
import com.alibaba.fastjson.JSON;
-
import lombok.Data;
-
-
import java.io.Serializable;
-
import java.util.HashMap;
-
import java.util.Map;
-
-
/**
-
* lombok格式化数据
-
*/
-
@Data
-
public
class JsonResult implements Serializable{
-
-
private
int code;
//返回码 非0即失败
-
private String msg;
//消息提示
-
private Map<String, Object> data;
//返回的数据
-
-
public JsonResult(){};
-
-
public JsonResult(int code, String msg, Map<String, Object> data) {
-
this.code = code;
-
this.msg = msg;
-
this.data = data;
-
}
-
-
public static String success() {
-
return success(
new HashMap(
0));
-
}
-
public static String success(Map<String, Object> data) {
-
return JSON.toJSONString(
new JsonResult(
0,
"解析成功", data));
-
}
-
-
public static String failed() {
-
return failed(
"解析失败");
-
}
-
public static String failed(String msg) {
-
return failed(-
1, msg);
-
}
-
public static String failed(int code, String msg) {
-
return JSON.toJSONString(
new JsonResult(code, msg,
new HashMap(
0)));
-
}
-
-
}
接下来定义一个全局异常处理类:
-
package com.springboot.exception.global_excep;
-
-
import com.springboot.exception.json.JsonResult;
-
import org.slf4j.Logger;
-
import org.slf4j.LoggerFactory;
-
import org.springframework.beans.ConversionNotSupportedException;
-
import org.springframework.beans.TypeMismatchException;
-
import org.springframework.http.converter.HttpMessageNotReadableException;
-
import org.springframework.http.converter.HttpMessageNotWritableException;
-
import org.springframework.web.HttpMediaTypeNotAcceptableException;
-
import org.springframework.web.HttpRequestMethodNotSupportedException;
-
import org.springframework.web.bind.MissingServletRequestParameterException;
-
import org.springframework.web.bind.annotation.ControllerAdvice;
-
import org.springframework.web.bind.annotation.ExceptionHandler;
-
import org.springframework.web.bind.annotation.ResponseBody;
-
-
import java.io.IOException;
-
-
/**
-
* @Author 18011618
-
* @Description 全局异常拦截器
-
* @Date 16:38 2018/7/5
-
* @Modify By
-
*/
-
@ControllerAdvice
-
@ResponseBody
-
public
class GlobalExceptionHandler {
-
-
private
static
final String logExceptionFormat =
"Capture Exception By GlobalExceptionHandler: Code: %s Detail: %s";
-
private
static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
-
-
//运行时异常
-
@ExceptionHandler(RuntimeException.class)
-
public String runtimeExceptionHandler(RuntimeException ex) {
-
return resultFormat(
1, ex);
-
}
-
-
//空指针异常
-
@ExceptionHandler(NullPointerException.class)
-
public String nullPointerExceptionHandler(NullPointerException ex) {
-
System.err.println(
"NullPointerException:");
-
return resultFormat(
2, ex);
-
}
-
-
//类型转换异常
-
@ExceptionHandler(ClassCastException.class)
-
public String classCastExceptionHandler(ClassCastException ex) {
-
return resultFormat(
3, ex);
-
}
-
-
//IO异常
-
@ExceptionHandler(IOException.class)
-
public String iOExceptionHandler(IOException ex) {
-
return resultFormat(
4, ex);
-
}
-
-
//未知方法异常
-
@ExceptionHandler(NoSuchMethodException.class)
-
public String noSuchMethodExceptionHandler(NoSuchMethodException ex) {
-
return resultFormat(
5, ex);
-
}
-
-
//数组越界异常
-
@ExceptionHandler(IndexOutOfBoundsException.class)
-
public String indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
-
return resultFormat(
6, ex);
-
}
-
-
//400错误
-
@ExceptionHandler({HttpMessageNotReadableException.class})
-
public String requestNotReadable(HttpMessageNotReadableException ex) {
-
System.out.println(
"400..requestNotReadable");
-
return resultFormat(
7, ex);
-
}
-
-
//400错误
-
@ExceptionHandler({TypeMismatchException.class})
-
public String requestTypeMismatch(TypeMismatchException ex) {
-
System.out.println(
"400..TypeMismatchException");
-
return resultFormat(
8, ex);
-
}
-
-
//400错误
-
@ExceptionHandler({MissingServletRequestParameterException.class})
-
public String requestMissingServletRequest(MissingServletRequestParameterException ex) {
-
System.out.println(
"400..MissingServletRequest");
-
return resultFormat(
9, ex);
-
}
-
-
//405错误
-
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
-
public String request405(HttpRequestMethodNotSupportedException ex) {
-
return resultFormat(
10, ex);
-
}
-
-
//406错误
-
@ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
-
public String request406(HttpMediaTypeNotAcceptableException ex) {
-
System.out.println(
"406...");
-
return resultFormat(
11, ex);
-
}
-
-
//500错误
-
@ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
-
public String server500(RuntimeException ex) {
-
System.out.println(
"500...");
-
return resultFormat(
12, ex);
-
}
-
-
//栈溢出
-
@ExceptionHandler({StackOverflowError.class})
-
public String requestStackOverflow(StackOverflowError ex) {
-
return resultFormat(
13, ex);
-
}
-
-
//除数不能为0
-
@ExceptionHandler({ArithmeticException.class})
-
public String arithmeticException(ArithmeticException ex) {
-
return resultFormat(
13, ex);
-
}
-
-
-
//其他错误
-
@ExceptionHandler({Exception.class})
-
public String exception(Exception ex) {
-
return resultFormat(
14, ex);
-
}
-
-
private <T extends Throwable>
String resultFormat(Integer code, T ex) {
-
ex.printStackTrace();
-
log.error(String.format(logExceptionFormat, code, ex.getMessage()));
-
return JsonResult.failed(code, ex.getMessage());
-
}
-
-
}
代码写完了,讲解一下几个新的注解:
@ControllerAdvice: 可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute,并应用到所有@RequestMapping中
@ExceptionHandler:标识异常类型对应的处理方法
比如下面这个方法:就是处理当遇到了空指针异常对应的处理方法
@ExceptionHandler(NullPointerException.class) public String nullPointerExceptionHandler(NullPointerException ex) { System.err.println("NullPointerException:"); return resultFormat(2, ex); }
接下来写一个测试的controller:
-
package com.springboot.exception.controller;
-
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
/**
-
* @Author 18011618
-
* @Date 15:59 2018/7/13
-
* @Function 测试异常的controller
-
*/
-
@RestController
-
public
class ExceptionController {
-
-
@RequestMapping(
"/exce")
-
public String showInfo(){
-
System.err.println(
"dddddddddddddd");
-
String info =
"你好";
-
int a =
1/
0;
-
return info;
-
}
-
}
最看看一下对应的application.yaml配置文件
server: port: 8888
最后写一个启动应用类:
-
package com.springboot.exception;
-
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-
/**
-
* @Author 18011618
-
* @Description
-
* @Date 16:00 2018/7/13
-
* @Modify By
-
*/
-
@SpringBootApplication
-
public
class ExceptionApplication {
-
public static void main(String[] args) {
-
SpringApplication.run(ExceptionApplication.class,args);
-
}
-
}
这个时候启动应用类之后,然后访问浏览器 http://localhost:8888/exce 对应的效果如下
这个返回的消息就是全局异常帮忙处理的。其它异常也可以去测试 都是可以拦截的,这里要注意一下:
如果不想用@ResponseBody的话,可以使用@RestControllerAdvice代替@ControllerAdvice
OK 效果实现了,它主要是在Spring mvc 启动时调用RequestMappingHandlerAdapter类的initControllerAdviceCache()方法进行初始化
到此为止,全局异常处理就讲解完了
版权声明:转载请标明博客地址,谢谢!