Fork me on Gitee

SpringBoot项目开发 -- 引入统一的异常处理类、响应类

统一响应对象

Result

package com.XXX.blog.framework.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @description: 响应对象
 * @author: XXX
 * @date: 2021/11/22 12:24
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Accessors(chain = true)
public class Result<T> {
    //响应码
    private String retCode;
    //响应信息
    private String retMsg;
    //响应数据
    private T retData;
}

ResultEnum 枚举

package com.xxx.blog.framework.response;

/**
 * @description: 响应相关的枚举对象
 * @author: xxx
 * @date: 2021/11/22 13:53
 */
public enum ResultEnum {
    SUCCESS("200","执行成功"),
    ERROR("-100","系统异常"),
    DB_ERROR("-101","数据库异常");

    private final String code;
    private final String msg;
    private ResultEnum(String code,String msg){
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

ResultUtil

package com.xxx.blog.framework.response;

/**
 * @description: 响应对象封装工具类
 * @author: xxx
 * @date: 2021/11/22 14:36
 */
public class ResultUtil {

    /**
     * 自定义通用响应
     * : 声明为私有,表示对外返回时只能是[SUCCESS or ERROR]
     * @param code
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    private static <T> Result<T> result(String code, String msg, T data){
        return new Result<T>(code,msg,data);
    }

    /**
     * 各种不同的SUCCESS响应,成功只有一种枚举类型
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(){
        return result(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),null);
    }
    public static <T> Result<T> success(String msg){
        return result(ResultEnum.SUCCESS.getCode(),msg,null);
    }
    public static <T> Result<T> success(T data){
        return result(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),data);
    }
    public static <T> Result<T> success(String msg, T data){
        return result(ResultEnum.SUCCESS.getCode(),msg,data);
    }

    /**
     * 各种不同的ERROR响应,失败有多种类型
     * @param <T>
     * @return
     */
    public static <T> Result<T> error(){
        return result(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(),null);
    }
    public static <T> Result<T> error(String msg){
        return result(ResultEnum.ERROR.getCode(),msg,null);
    }
    public static <T> Result<T> error(T data){
        return result(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(),data);
    }
    public static <T> Result<T> error(String msg, T data){
        return result(ResultEnum.ERROR.getCode(),msg,data);
    }
    public static <T> Result<T> error(ResultEnum resultEnum, T data){
        return result(resultEnum.getCode(),resultEnum.getMsg(),data);
    }
    public static <T> Result<T> error(ResultEnum resultEnum){
        return result(resultEnum.getCode(),resultEnum.getMsg(),null);
    }

}

实际使用

/**
     * 获取所有Menu信息
     * @return
     */
    @Log
    @RequestMapping(value = "/getAllMenu",produces = "application/json")
    public Result<List<UmsMenu>> getAllMenu(){
        return ResultUtil.success(webService.getAll());
    }

统一异常处理类

GlobalExceptionHandler

package com.xxx.blog.framework.exception;

import com.xxx.blog.framework.response.Result;
import com.xxx.blog.framework.response.ResultEnum;
import com.xxx.blog.framework.response.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description: 统一异常处理器
 * @author: xxx
 * @date: 2021/11/23 10:33
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 业务异常
     * @param e
     * @return
     */
    @ExceptionHandler({BizException.class})
    @ResponseBody
    public Result bizExceptionHandler(BizException e){
        log.error("业务异常,原因是:{}",e.getMessage());
        return ResultUtil.error(e.getMessage());
    }

    /**
     * 其他任意异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public Result exceptionHandler(Exception e){
        log.error("系统异常,原因是:",e);
        //e.printStackTrace(); // 打印异常栈
        return ResultUtil.error(ResultEnum.ERROR);
    }
}
posted @ 2021-12-15 23:51  江南西道  阅读(125)  评论(0编辑  收藏  举报
Fork me on GitHub