springboot返回统一接口与统一异常处理
在做后端服务开发时,想要每次获取的数据和抛出的异常保持统一的返回结果,结构清晰,方便管理
1. 封装统一返回格式工具类ResultUtil
//引入Lombok依赖,不引入可以自己构建getter,setter
@Data
@Component
public class ResultUtil<T> {
//三个主要参数,code码,消息体,数据体
private Integer code;
private String msg;
private T data;
//定义默认成功code码
private static final Integer SUCCESS_CODE =10000;
//定义默认成功msg
private static final String SUCCESS_MSG = "SUCCESS";
//全参构造函数
public ResultUtil(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
//两参数构造函数
public ResultUtil(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
//无参构造函数
public ResultUtil(){
//在success()无参传入时调用
this(SUCCESS_CODE,SUCCESS_MSG);
}
//默认成功返回,比如增加、删除、修改
public static ResultUtil success(){
//直接调用无参构造函数
return new ResultUtil();
}
//带参成功返回,比如查询
public static<T> ResultUtil<T> success(T data){
ResultUtil<T> resultUtil = new ResultUtil<>();
resultUtil.setData(data);
return resultUtil;
}
//默认失败返回,需要传入code,msg,但是这样我们需要记录各类异常比较麻烦,可以直接引入异常枚举类,如下
public static<T> ResultUtil<T> error(Integer code,String msg){
return new ResultUtil<>(code,msg);
}
//传入异常枚举类
public static<T> ResultUtil<T> error(CodeMsgEnum ex){
return new ResultUtil<>(ex.getCode(),ex.getMsg());
}
}
2. 自定义异常枚举类
public enum CodeMsgEnum {
//各类错误信息,可以存储在这里
COMMON_ERROR(10001,"通用错误"),
USER_EXITS(10002,"用户已存在"),
SYSTEM_ERROR(20000,"系统异常");
private Integer code;
private String msg;
//构造函数,也可以使用lombok的 @AllArgsConstructor 全参构造器
CodeMsgEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
//下面是getter,setter方法,也可以使用lombok的@Getter,@Setter
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
public void setCode(Integer code) {
this.code = code;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
3. 测试
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/get/user")
public ResultUtil UserInfo(@RequestParam Integer id){
return ResultUtil.success();
return ResultUtil.success( userService.getUser(id));
return ResultUtil.error(CodeMsgEnum.COMMON_ERROR);
}
}
4. 自定义异常类
@Data
public class CommonException extends Exception {
private Integer code;
private String msg;
CommonException(Integer code,String msg){
this.code = code;
this.msg = msg;
}
public CommonException(CodeMsgEnum ex){
this(ex.getCode(),ex.getMsg());
}
}
5. 统一异常处理
//ControllerAdvice是Controller增强器,可对controller中被 @RequestMapping注解的方法加一些逻辑处理
@ControllerAdvice
public class GlobalExceptionHandler {
//处理系统异常
@ExceptionHandler(value = Exception.class)
@ResponseBody
public ResultUtil SystemHandle(Exception e) {
return ResultUtil.error(CodeMsgEnum.SYSTEM_ERROR);
}
//处理自定义异常
@ExceptionHandler(value = CommonException.class)
@ResponseBody
public ResultUtil CommonHandle(CommonException e) {
return ResultUtil.error(e.getCode(),e.getMsg());
}
}
6. 测试异常
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/get/user")
public ResultUtil UserInfo(@RequestParam Integer id){
//测试内部异常
if(true){
throw new CommonException(CodeMsgEnum.USER_EXITS);
}
return ResultUtil.success();
//测试系统异常
Integer a = 1;
Integer b = 0;
Integer c = a/b;
return ResultUtil.success();
}
}