三、全局异常Exception的配置
一、首先自定义一个异常拦截器
1、拦截异常所有 及拦截 Exception异常
package com.cg.demo.exception;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* 说明:
*
* @auther panhh
* @date 2019/7/12 9:37
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
//捕获所有异常 获取内部异常处理类:Exception
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = Exception.class)
public Map excption(Exception e, HttpServletRequest request){
String eMessage = e.getMessage();
StringBuffer url = request.getRequestURL();
System.out.println("ErrorMsg-->"+eMessage);
System.out.println("url-->"+url);
//打印日志
LOG.error("错误信息-->"+eMessage+"===请求链接--->"+url);
HashMap<Object, Object> hashMap = new HashMap<>();
hashMap.put("msg",eMessage);
hashMap.put("url",url);
return hashMap;
}
}
2、效果测试
@Controller
public class ExcTestController {
@RequestMapping("/test")
@ResponseBody
public String test(){
int a = 4/0;
return "test";
}
}
2、拦截器效果(控制台打印)
ErrorMsg-->/ by zero
url-->http://localhost:8080/test
2019-07-12 09:59:11.124 ERROR 10096 --- [nio-8080-exec-5] c.c.d.exception.GlobalExceptionHandler : 错误信息-->/ by zero===请求链接--->http://localhost:8080/test
3、页面返回内容:
{"msg":"/ by zero","url":"http://localhost:8080/test"}
二、自定义一个异常,自定义异常需要自己抛出
1、自定义一个异常
package com.cg.demo.exception;
import lombok.Data;
import lombok.ToString;
/**
* 说明:
*
* @auther panhh
* @date 2019/7/12 9:36
*/
@Data
@ToString
public class GlobalException extends RuntimeException {
private String msg;
private Integer code;
public GlobalException(String msg, Integer code) {
this.msg = msg;
this.code = code;
}
}
2、异常拦截器拦截方法
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = GlobalException.class)
public Map myExcption(GlobalException e, HttpServletRequest request){
String eMessage = e.getMsg();
Integer code = e.getCode();
StringBuffer url = request.getRequestURL();
System.out.println("ErrorMsg-->"+eMessage);
System.out.println("url-->"+url);
//打印日志
LOG.error("错误信息-->"+eMessage+"===请求链接--->"+url+"错误状态码--->"+code);
HashMap<Object, Object> hashMap = new HashMap<>();
hashMap.put("msg",eMessage);
hashMap.put("code",code);
hashMap.put("url",url);
return hashMap;
}
3、测试
(1)
@Controller
public class ExcTestController {
@RequestMapping("/test")
@ResponseBody
public String test() {
throw new GlobalException("抛出一个自定义异常",4001);
}
}
(2)拦截器打印内容
ErrorMsg-->抛出一个自定义异常
url-->http://localhost:8080/test
2019-07-12 10:14:01.155 ERROR 10132 --- [nio-8080-exec-1] c.c.d.exception.GlobalExceptionHandler : 错误信息-->抛出一个自定义异常===请求链接--->http://localhost:8080/test错误状态码--->4001
(3)前台页面返回
{"msg":"抛出一个自定义异常","code":4001,"url":"http://localhost:8080/test"}
三、补充,可以自定义一个返回信息枚举类
1、自定义一个枚举类
package com.cg.demo.exception;
import lombok.Data;
/**
* 说明:
*
* @auther panhh
* @date 2019/7/12 10:21
*/
public enum ResponseEnum {
SUCCESS(200,"操作成功"),
ERROR(400,"操作失败"),
;
private Integer code;
private String msg;
ResponseEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
2、自定义异常类修改 GlobalException为
package com.cg.demo.exception;
import lombok.Data;
import lombok.ToString;
/**
* 说明:
*
* @auther panhh
* @date 2019/7/12 9:36
*/
@Data
@ToString
public class GlobalException extends RuntimeException {
private ResponseEnum responseEnum;
//添加一个构造函数
public GlobalException(ResponseEnum re){
this.responseEnum = re;
}
}
3、异常拦截器拦截方法修改为
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = GlobalException.class)
public Map myExcption(GlobalException e, HttpServletRequest request){
ResponseEnum re = e.getResponseEnum();
String eMessage = re.getMsg();
Integer code = re.getCode();
StringBuffer url = request.getRequestURL();
System.out.println("ErrorMsg-->"+eMessage);
System.out.println("code-->"+code);
System.out.println("url-->"+url);
//打印日志
LOG.error("错误信息-->"+eMessage+"===请求链接--->"+url+"错误状态码--->"+code);
HashMap<Object, Object> hashMap = new HashMap<>();
hashMap.put("msg",eMessage);
hashMap.put("code",code);
hashMap.put("url",url);
return hashMap;
}
4、异常演示
(1)、抛出一个异常
@Controller
public class ExcTestController {
@RequestMapping("/test")
@ResponseBody
public String test() {
throw new GlobalException(ResponseEnum.ERROR);
}
}
(2)、拦截器方法打印的内容
ErrorMsg-->操作失败
code-->400
url-->http://localhost:8080/test
019-07-12 10:31:06.281 ERROR 4764 --- [nio-8080-exec-1] c.c.d.exception.GlobalExceptionHandler : 错误信息-->操作失败===请求链接--->http://localhost:8080/test错误状态码--->400
(3)、前台页面返回内容
{"msg":"操作失败","code":400,"url":"http://localhost:8080/test"}
四、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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cg.demo</groupId>
<artifactId>some_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>some_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--Aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
五、本文到这里就结束了,奋斗越久越划算,工资变成零花钱 ,希望你不忘初心,继续努力哈!