@ControllerAdvice,是Spring3.2提供的新注解,它是一个 Controller 增强器,可对 controller 中被 @RequestMapping 注解的方法加一些逻辑处理,这里我们使用它来做全局异常处理。
@RestControllerAdvice ,ControllerAdvice 的增强版,包含了@ControllerAdvice 和 @ResponseBody 注解功能;默认处理所有 controller 中被 @RequestMapping注解的方法,并且返回的数据为 json 格式
@ExceptionHandler ,使用此注解来指定获取自定义异常类
创建 springboot 项目后,引入基本 web 依赖,否则注解会打不出来的哦
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
package com.wxw.springboot_exception.exception;
/**
* @author wuxiongwei
* @date 2020/5/27 10:45
* @Description
*/
public class BaseException extends RuntimeException {
private static final long serialVersionUID = 4397143116579765052L;
/**
* 错误代码
*/
private String code;
/**
* 错误信息
*/
private String message;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.wxw.springboot_exception.exception;
/**
* @author wuxiongwei
* @date 2020/5/27 10:57
* @Description
*/
public class CustomException extends BaseException {
private static final long serialVersionUID = -7796809450875600938L;
public CustomException(String code,String message) {
setCode(code);
setMessage(message);
}
public CustomException(String message) {
setCode("");
setMessage(message);
}
public CustomException() {
}
}
package com.wxw.springboot_exception.base;
import com.wxw.springboot_exception.exception.CustomException;
/**
* @author wuxiongwei
* @date 2020/5/27 11:03
* @Description
*/
public class Assert {
public static void assertTrue(boolean b, String msg, Object... args) {
if (b) {
fail(msg, args);
}
}
public static void assertFalse(boolean b, String msg, Object... args) {
if (!b) {
fail(msg, args);
}
}
public static void fail(String msg, Object... args) {
throw new CustomException(String.format(msg, args));
}
}
package com.wxw.springboot_exception.base;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author wuxiongwei
* @date 2020/5/27 11:07
* @Description 接口返回统一封装类
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResultResponse<T> {
/**
* 返回状态码
*/
private Integer status;
/**
* 返回信息
*/
private String message;
/**
* 返回数据
*/
private T data;
/**
* token
*/
private String token;
}
package com.wxw.springboot_exception.Enums;
/**
* @author wuxiongwei
* @date 2020/5/27 11:25
* @Description
*/
public enum ResultCodeEnum {
SUCCESS(200,"请求成功"),
FAILURE(500,"请求失败"),
;
private Integer code;
private String message;
ResultCodeEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.wxw.springboot_exception.exception;
import com.wxw.springboot_exception.Enums.ResultCodeEnum;
import com.wxw.springboot_exception.base.ResultResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 统一全局异常处理类
*/
@RestControllerAdvice //ControllerAdvice 的增强版,包含了@ControllerAdvice 和 @ResponseBody 注解功能;默认处理所有 controller 中被 @RequestMapping注解的方法
//@RestControllerAdvice(basePackages = "com.wxw.springboot_exception.controller") //指定处理 controller 包
//@RestControllerAdvice(basePackageClasses = BController.class) // 指定处理的 controller 类
//@ControllerAdvice()
public class GlobalMyExceptionHandler {
@ExceptionHandler(value = CustomException.class)//使用此注解来指定获取自定义异常类,返回给前端 json 格式错误数据
public ResultResponse handlerException(CustomException e){
return ResultResponse.builder().status(ResultCodeEnum.FAILURE.getCode()).message(e.getMessage()).build();
}
}
package com.wxw.springboot_exception.controller;
import com.wxw.springboot_exception.Enums.ResultCodeEnum;
import com.wxw.springboot_exception.base.Assert;
import com.wxw.springboot_exception.base.ResultResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wuxiongwei
* @date 2020/5/27 11:14
* @Description
*/
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/test")
public ResultResponse test(){
//使用断言方法,取代 if 判断,同时抛出异常
Assert.assertTrue(1 == 1,"TEST测试异常");
return ResultResponse.builder().status(ResultCodeEnum.SUCCESS.getCode()).message(ResultCodeEnum.SUCCESS.getMessage()).data("hello").build();
}
}
使用 postman 请求接口,进行测试,可以看到我们测试的异常已经被以 json 数据返回出来了,
证明我们自定义异常已经成功被拦截处理。