1. 统一返回结果集
package com.liming.pojo;
import com.liming.exception.AppExceptionCodeMsg;
import lombok.Data;
/**
* 全局统一返回结果类
* @author 黎明
* @date 2023/9/6 14:11
* @version 1.0
*/
@Data
public class Result<T> {
private Integer code; // 状态码
private String msg; //返回消息
private T data; //返回数据
/**
* 私有化构造方法,禁止在其它类创建对象
*/
private Result(Integer code,String msg,T data){
this.code = code;
this.msg = msg;
this.data = data;
}
// 不带自定义消息响应成功的方法
public static <T> Result success(T data){
Result resp = new Result(200, "success", data);
return resp;
}
// 带自定义消息响应成功的方法
public static <T> Result success(String msg, T data){
Result resp = new Result(200,msg, data);
return resp;
}
// 自定义异常返回的结果
public static <T> Result error(AppExceptionCodeMsg appExceptionCodeMsg){
Result resp = new Result(appExceptionCodeMsg.getCode(), appExceptionCodeMsg.getMsg(), null);
return resp;
}
// 其他异常返回的结果
public static <T> Result error(Integer code, String msg){
Result resp = new Result(code,msg, null);
return resp;
}
}
2.业务相关异常枚举类
package com.liming.exception;
/**
* 这个枚举类中定义的都是跟业务有关的异常
* @author 黎明
* @date 2023/9/6 14:31
* @version 1.0
*/
public enum AppExceptionCodeMsg {
// 业务操作错误定义
INVALID_CODE(10000,"验证码无效"),
USERNAME_NOT_EXISTS(10001,"用户名不存在"),
USER_CREDIT_NOT_ENOUTH(10002,"用户积分不足"),
;
// 错误码
private Integer code ;
// 错误消息
private String msg ;
AppExceptionCodeMsg(Integer code, String msg){
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
3.自定义异常类
package com.liming.exception;
/**
* 自定义异常类
*
* @author 黎明
* @version 1.0
* @date 2023/9/6 14:45
*/
public class AppException extends RuntimeException {
// 错误码
private Integer code;
// 异常的消息描述
private String msg;
public AppException(AppExceptionCodeMsg appExceptionCodeMsg) {
super();
this.code = appExceptionCodeMsg.getCode();
this.msg = appExceptionCodeMsg.getMsg();
}
public AppException(Integer code, String msg) {
super();
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
4.统一异常处理类
package com.liming.exception;
import com.liming.pojo.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 全局统一异常处理类
*
* @author 黎明
* @version 1.0
* @date 2023/9/6 14:42
*/
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = {Exception.class})
@ResponseBody
public <T> Result<T> exceptionHandler(Exception e) {
// 这里先判断拦截到的Exception是不是我们自定义的异常类型
if (e instanceof AppException) {
AppException appException = (AppException) e;
return Result.error(appException.getCode(), appException.getMsg());
}
// 如果拦截的异常不是我们自定义的异常(例如:数据库主键冲突)
return Result.error(500, "服务器端异常");
}
}
5.测试
controller
package com.liming.controller;
import com.liming.exception.AppException;
import com.liming.exception.AppExceptionCodeMsg;
import com.liming.pojo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
/**
* @author 黎明
* @version 1.0
* @date 2023/9/6 14:53
*/
@Api(tags = "统一异常处理接口")
@RestController
public class UnifiedExceptionController {
@ApiOperation("异常测试")
@GetMapping("/demo")
public Result<String> demo1(String name) {
if ("ok".equals(name)) {
return Result.success("succ");
}
if ("err".equals(name)) {
//抛自定义业务相关的异常
throw new AppException(AppExceptionCodeMsg.USERNAME_NOT_EXISTS);
}
if ("errCode".equals(name)) {
//抛自定义业务相关的异常
throw new AppException(AppExceptionCodeMsg.INVALID_CODE);
}
if("notenough".equals(name)){
//抛自定义业务相关的异常
throw new AppException(AppExceptionCodeMsg.USER_CREDIT_NOT_ENOUTH);
}
if ("0".equals(name)) {
// 抛出其他异常
int i = 1 / 0;
}
return Result.success("操作成功");
}
@GetMapping("list")
public Result<List> list(){
List<String> list = Arrays.asList("zhangsan","lisi","wangwu");
return Result.success(list);
}
}
结果展示