开发必备:响应封装、异常封装、异常捕获

整合 fastjson

让返回的 json 更漂亮

SpringBoot 整合 fastjson

响应封装

@Data
public final class BaseResponseVO<M> {

    private BaseResponseVO(){}

    // 返回状态【0-成功,1-业务失败,999-表示系统异常】
    private int status;
    // 返回信息
    private String msg;
    // 返回数据实体;

    private M data;
    // 图片前缀
    private String imgPre;

    // 分页使用
    private Long nowPage;
    private Long totalPage;


    public static<M> BaseResponseVO success(){
        BaseResponseVO baseResponseVO = new BaseResponseVO();
        baseResponseVO.setStatus(0);
        return baseResponseVO;
    }

    public static<M> BaseResponseVO success(String msg){
        BaseResponseVO baseResponseVO = new BaseResponseVO();
        baseResponseVO.setStatus(0);
        baseResponseVO.setMsg(msg);
        return baseResponseVO;
    }

    public static<M> BaseResponseVO success(M data){
        BaseResponseVO baseResponseVO = new BaseResponseVO();
        baseResponseVO.setStatus(0);
        baseResponseVO.setData(data);
        return baseResponseVO;
    }

    public static<M> BaseResponseVO success(long nowPage,long totalPage,M m){
        BaseResponseVO responseVO = new BaseResponseVO();
        responseVO.setStatus(0);
        responseVO.setData(m);
        responseVO.setTotalPage(totalPage);
        responseVO.setNowPage(nowPage);

        return responseVO;
    }

    public static<M> BaseResponseVO success(long nowPage,long totalPage,String imgPre,M m){
        BaseResponseVO responseVO = new BaseResponseVO();
        responseVO.setStatus(0);
        responseVO.setData(m);
        responseVO.setImgPre(imgPre);
        responseVO.setTotalPage(totalPage);
        responseVO.setNowPage(nowPage);

        return responseVO;
    }

    public static<M> BaseResponseVO success(String imgPre,M m){
        BaseResponseVO responseVO = new BaseResponseVO();
        responseVO.setStatus(0);
        responseVO.setData(m);
        responseVO.setImgPre(imgPre);

        return responseVO;
    }


    public static<M> BaseResponseVO noLogin(){
        BaseResponseVO responseVO = new BaseResponseVO();
        responseVO.setStatus(700);
        responseVO.setMsg("用户需要登陆");

        return responseVO;
    }

    public static<M> BaseResponseVO serviceFailed(int status,String msg){
        BaseResponseVO baseResponseVO = new BaseResponseVO();
        baseResponseVO.setStatus(status);
        baseResponseVO.setMsg(msg);
        return baseResponseVO;
    }

    public static<M> BaseResponseVO serviceFailed(String msg){
        BaseResponseVO baseResponseVO = new BaseResponseVO();
        baseResponseVO.setStatus(1);
        baseResponseVO.setMsg(msg);
        return baseResponseVO;
    }

    public static<M> BaseResponseVO serviceFailed(String msg,M data){
        BaseResponseVO baseResponseVO = new BaseResponseVO();
        baseResponseVO.setStatus(1);
        baseResponseVO.setMsg(msg);
        baseResponseVO.setData(data);
        return baseResponseVO;
    }


    public static<M> BaseResponseVO systemError(){
        BaseResponseVO baseResponseVO = new BaseResponseVO();
        baseResponseVO.setStatus(999);
        baseResponseVO.setMsg("系统异常,请联系管理员");
        return baseResponseVO;
    }

}

异常封装

Service 层

业务异常

@Data
public class CommonServiceExcetion extends Exception{

    private Integer code;
    private String errMsg;

    public CommonServiceExcetion(int code, String errMsg){
        super(errMsg);
        this.code = code;
        this.errMsg = errMsg;
    }

}

数据异常

@Data
public class DataTranctionExcetion extends Exception{

    private Integer code;
    private String errMsg;

    public DataTranctionExcetion(int code,String errMsg){
        super(errMsg);
        this.code = code;
        this.errMsg = errMsg;
    }

}

Controller 层

参数验证异常

@Data
public class ParamErrorException extends Exception {

    private Integer code;
    private String errMsg;

    public ParamErrorException(int code, String errMsg){
        super(errMsg);
        this.code = code;
        this.errMsg = errMsg;
    }

}

异常捕获

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(CommonServiceExcetion.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public BaseResponseVO commonServiceException(CommonServiceExcetion e){
        return BaseResponseVO.serviceFailed(e.getCode(),e.getErrMsg());
    }

    @ExceptionHandler(ParamErrorException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public BaseResponseVO paramErrorException(ParamErrorException e){
        return BaseResponseVO.serviceFailed(e.getCode(),e.getErrMsg());
    }


    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public BaseResponseVO exception(ExceptionHandler e){
        return BaseResponseVO.systemError();
    }
    
}
©️2020 CSDN 皮肤主题: 岁月 设计师: pinMode 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值