Springboot front and back end unified data interaction mode + unified exception handling

1 Unified data interaction method between front and back ends

1.1 Unified result set

package com.zhmsky.result;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author zhmsky
 * @description 前后端数据统一交互方式
 * @date 2022/3/21 0:50
 */
@Data
public class Result<T> implements Serializable {
    
    

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "成功标志")
    private boolean success;

    @ApiModelProperty(value = "消息")
    private String message;

    @ApiModelProperty(value = "返回代码")
    private Integer code;

    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    @ApiModelProperty(value = "结果对象")
    private T result;
}

1.2 Result set return tool class

package com.zhmsky.result;

/**
 * @author zhmsky
 * @date 2022/3/21 0:52
 */
public class ResultUtil<T> {
    
    

    private Result<T> result;

    public ResultUtil() {
    
    
        result = new Result<>();
        result.setSuccess(true);
        result.setMessage("success");
        result.setCode(200);
    }

    public Result<T> setData(T t) {
    
    
        this.result.setResult(t);
        this.result.setCode(200);
        return this.result;
    }

    public Result<T> setSuccessMsg(String msg) {
    
    
        this.result.setSuccess(true);
        this.result.setMessage(msg);
        this.result.setCode(200);
        this.result.setResult(null);
        return this.result;
    }

    public Result<T> setData(T t, String msg) {
    
    
        this.result.setResult(t);
        this.result.setCode(200);
        this.result.setMessage(msg);
        return this.result;
    }

    public Result<T> setErrorMsg(String msg) {
    
    
        this.result.setSuccess(false);
        this.result.setMessage(msg);
        this.result.setCode(500);
        return this.result;
    }

    public Result<T> setErrorMsg(Integer code, String msg) {
    
    
        this.result.setSuccess(false);
        this.result.setMessage(msg);
        this.result.setCode(code);
        return this.result;
    }

    public static <T> Result<T> data(T t) {
    
    
        return new ResultUtil<T>().setData(t);
    }

    public static <T> Result<T> data(T t, String msg) {
    
    
        return new ResultUtil<T>().setData(t, msg);
    }

    public static <T> Result<T> success(String msg) {
    
    
        return new ResultUtil<T>().setSuccessMsg(msg);
    }

    public static <T> Result<T> error(String msg) {
    
    
        return new ResultUtil<T>().setErrorMsg(msg);
    }

    public static <T> Result<T> error(Integer code, String msg) {
    
    
        return new ResultUtil<T>().setErrorMsg(code, msg);
    }
}

2 Unified handling of exceptions

2.1 Global exception handling

package com.zhmsky.exception.handler;

import com.zhmsky.exception.MyException;
import com.zhmsky.result.Result;
import com.zhmsky.result.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author zhmsky
 * @description 统一异常处理类
 * @date 2022/3/21 16:18
 */
@ControllerAdvice
public class ExceptionHandler {
    
    

    /**
     * 全局异常处理
     * @param e
     * @return
     */
    @ResponseBody
    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    public Result<String> error(Exception e){
    
    
        e.printStackTrace();
        return new ResultUtil<String>().setErrorMsg("发生未知错误!请联系管理员");
    }
}

2.2 Specific exception handling

package com.zhmsky.exception.handler;

import com.zhmsky.exception.MyException;
import com.zhmsky.result.Result;
import com.zhmsky.result.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author zhmsky
 * @description 统一异常处理类
 * @date 2022/3/21 16:18
 */
@ControllerAdvice
public class ExceptionHandler {
    
    

    /**
     * 特定异常处理
     * @param e
     * @return
     */
    @ResponseBody
    @org.springframework.web.bind.annotation.ExceptionHandler(ArithmeticException.class)
    public Result<String> error(ArithmeticException e){
    
    
        e.printStackTrace();
        return new ResultUtil<String>().setErrorMsg("算数异常!请重新输入!");
    }

}

2.3 Custom exception handling

2.3.1 Custom exception class

package com.zhmsky.exception;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author zhmsky
 * @date 2022/3/21 17:00
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyException extends RuntimeException{
    
    

    @ApiModelProperty("状态码")
    private Integer code;

    @ApiModelProperty("异常消息")
    private String msg;
}

Custom exceptions need to be thrown manually where exceptions may occur in the program, for example:

   try {
    
    
            int i = 1 / 0;
        } catch (Exception e) {
    
    
            throw new MyException(20002,"自定义异常");
        }

2.3.2 Exception handling

package com.zhmsky.exception.handler;

import com.zhmsky.exception.MyException;
import com.zhmsky.result.Result;
import com.zhmsky.result.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author zhmsky
 * @description 统一异常处理类
 * @date 2022/3/21 16:18
 */
@ControllerAdvice
public class ExceptionHandler {
    
    
    /**
     * 自定义异常处理
     * @param e
     * @return
     */
    @ResponseBody
    @org.springframework.web.bind.annotation.ExceptionHandler(MyException.class)
    public Result<Map<String,Object>> error(MyException e){
    
    
        e.printStackTrace();
        return new ResultUtil<Map<String,Object>>().setErrorMsg(e.getMsg());
    }
}

Guess you like

Origin blog.csdn.net/weixin_42194695/article/details/123641082