欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot統(tǒng)一響應格式及統(tǒng)一異常處理

 更新時間:2023年05月11日 09:24:25   作者:JK凱  
在我們開發(fā)SpringBoot后端服務時,一般需要給前端統(tǒng)一響應格式,本文主要介紹了SpringBoot統(tǒng)一響應格式及統(tǒng)一異常處理

在我們開發(fā)SpringBoot后端服務時,一般需要給前端統(tǒng)一響應格式,方便前端調試及配置錯誤提示等等。這篇文章講講實際工作中統(tǒng)一響應格式及統(tǒng)一異常處理是如何做的。

一、統(tǒng)一響應基礎類

在項目中對應工具類或Vo層來創(chuàng)建我們的統(tǒng)一響應類

image.png

ResponseResult:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zhang.enums.AppHttpCodeEnum;
import java.io.Serializable;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;
    public ResponseResult() {
        this.code = AppHttpCodeEnum.SUCCESS.getCode();
        this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
    }
    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }
    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public static ResponseResult errorResult(int code, String msg) {
        ResponseResult result = new ResponseResult();
        return result.error(code, msg);
    }
    public static ResponseResult okResult() {
        ResponseResult result = new ResponseResult();
        return result;
    }
    public static ResponseResult okResult(int code, String msg) {
        ResponseResult result = new ResponseResult();
        return result.ok(code, null, msg);
    }
    public static ResponseResult okResult(Object data) {
        ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }
    public static ResponseResult errorResult(AppHttpCodeEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getMsg());
    }
    public static ResponseResult errorResult(AppHttpCodeEnum enums, String msg) {
        return setAppHttpCodeEnum(enums, msg);
    }
    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums) {
        return okResult(enums.getCode(), enums.getMsg());
    }
    private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg) {
        return okResult(enums.getCode(), msg);
    }
    public ResponseResult<?> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }
    public ResponseResult<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }
    public ResponseResult<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }
    public ResponseResult<?> ok(T data) {
        this.data = data;
        return this;
    }
    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
}

里面還有用到一個響應的枚舉類AppHttpCodeEnum,接下來我們創(chuàng)建這個枚舉類

二、響應枚舉類

image.png

AppHttpCodeEnum:

public enum AppHttpCodeEnum {
    // 成功
    SUCCESS(200, "操作成功"),
    // 登錄
    NEED_LOGIN(401, "需要登錄后操作"),
    NO_OPERATOR_AUTH(403, "無權限操作"),
    SYSTEM_ERROR(500, "出現(xiàn)錯誤"),
    USERNAME_EXIST(501, "用戶名已存在"),
    PHONENUMBER_EXIST(502, "手機號已存在"), EMAIL_EXIST(503, "郵箱已存在"),
    REQUIRE_USERNAME(504, "必需填寫用戶名"),
    CONTENT_NOT_NULL(506, "評論內容不能為空"),
    FILE_TYPE_ERROR(507, "文件類型錯誤"),
    USERNAME_NOT_NULL(508, "用戶名不能為空"),
    NICKNAME_NOT_NULL(509, "昵稱不能為空"),
    PASSWORD_NOT_NULL(510, "密碼不能為空"),
    EMAIL_NOT_NULL(511, "郵箱不能為空"),
    NICKNAME_EXIST(512, "昵稱已存在"),
    LOGIN_ERROR(505, "用戶名或密碼錯誤");
    int code;
    String msg;
    AppHttpCodeEnum(int code, String errorMessage) {
        this.code = code;
        this.msg = errorMessage;
    }
    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

一般我們在這個枚舉類中管理需要響應的錯誤codemsg

三、統(tǒng)一響應格式使用

在對應的controller或者service里面使用統(tǒng)一響應類

image.png

  • 成功: ResponseResult.okResult()
  • 失敗: ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR), 參數(shù)傳入我們定義的響應枚舉類

四、統(tǒng)一異常處理

1. 自定義異常

為什么我們需要自定義異常?因為在某些情況下,我們需要返回我們自定義的響應格式非常不方便,如在處理用戶鑒權或token校驗的時候,因為像這些部分我們一般都是在單獨的工具類中去處理,這時候如果報錯其實就可以拋出我們自定義的異常,交由我們全局的異常處理去統(tǒng)一返回響應。

  • exception包下新建SystemException
  • SystemException繼承RuntimeException

具體實現(xiàn)代碼如下

exception.SystemException:

import com.jk.enums.AppHttpCodeEnum;
public class SystemException extends RuntimeException{
    private int code;
    private String msg;
    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
    public SystemException(AppHttpCodeEnum appHttpCodeEnum) {
        super(appHttpCodeEnum.getMsg());
        this.code = appHttpCodeEnum.getCode();
        this.msg = appHttpCodeEnum.getMsg();
    }
}

目前只是自定義了異常,我們還需要對自定義的異常進行處理,返回統(tǒng)一的響應格式。

2.異常處理

  • handler.exception包下新建GlobalExceptionHandler處理類
  • @RestControllerAdvice是組合注解,由@ControllerAdvice、@ResponseBody組成,標明是一個統(tǒng)一異常處理的類,并把返回結果封裝在ResponseBody
  • @Slf4jLombok實現(xiàn)日志輸出的注解
  • @ExceptionHandler標明該方法處理哪些異常

具體代碼實現(xiàn)如下:

handler.exception.GlobalExceptionHandler:

import com.jk.enums.AppHttpCodeEnum;
import com.jk.exception.SystemException;
import com.jk.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(SystemException.class)
    public ResponseResult systemExceptionHandler(SystemException e) {
        log.error("出現(xiàn)了異常! {}", e);
        return ResponseResult.errorResult(e.getCode(), e.getMsg());
    }
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e) {
        log.error("出現(xiàn)了異常! {}", e);
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }
}

可以看到我們除了處理自定義異常SystemException外,還對Exception就是其他我們無法預料到的異常做了一個兜底。

3.自定義異常使用

在需要拋出異常的地方:

throw new SystemException(AppHttpCodeEnum.LOGIN_ERROR);

前端接收到的響應是:

{
    "code": 505,
    "msg": "用戶名或密碼錯誤"
}

這樣就比接收到500錯誤也不知道錯誤原因好多了。

到此這篇關于SpringBoot統(tǒng)一響應格式及統(tǒng)一異常處理的文章就介紹到這了,更多相關SpringBoot統(tǒng)一響應格式及異常處理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論