SpringBoot+Maven全局异常处理
- 在pom.xml中引入依赖文件
后面CodeMsg 类 需要用到该依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.44</version>
</dependency>
- 配置异常处理工具类
public class CodeMsg {
private int code; //返回码 非0即失败
private String msg; //消息提示
private Map<String, Object> data; //返回的数据
public CodeMsg(){};
public CodeMsg(int code, String msg, Map<String, Object> data) {
this.code = code;
this.msg = msg;
this.data = 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)));
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Map<String, Object> getData() {
return data;
}
public void setData(Map<String, Object> data) {
this.data = data;
}
}
- 配置全局异常处理类
@ControllerAdvice :可以用于定义 @ExceptionHandler
@ExceptionHandler :标识异常类型对应的处理方法
@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 CodeMsg.failed(code, ex.getMessage());
}
}
- 测试一下
@RestController
public class ExceptionController {
@RequestMapping("/hello")
public String helloController(){
int a = 1/0;
return "hello error";
}
当去请求:localhost:8080/hello的时候就会报错误