springboot返回結果包裝統一返回格式

风吹南下發表於2024-09-07

統一返回結果攔截處理類

import com.itcoder.test.utils.JsonUtils;
import com.sun.istack.internal.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.List;

/**
 * 對返回結果統一進行處理,包括返回結果格式統一包裝,返回異常統一處理
 *
 */
@Slf4j
@RestControllerAdvice
public class RestResponseAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }
    @Value("ignoreUris")
    private List<String> ignoreUris;
    /**
     * 返回結果包裝統一返回格式
     * @return 包裝後的返回結果
     */
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                  Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        if (ignoreUris.contains(request.getURI().getPath())){
            return body;
        }
        // 指定返回的結果為application/json格式
        // 不指定,String型別轉json後返回Content-Type是text/plain;charset=UTF-8
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        ResultJson result = new ResultJson(body);
        // 若返回型別是ResultJson,則不進行修改
        if (body == null) {
            if (returnType.getParameterType().isAssignableFrom(String.class)) {
                return JsonUtils.toJsonString(result);
            }
        }
        if (body instanceof ResultJson) {
            return body;
        }
        if (body instanceof String) {
            return  JsonUtils.toJsonString(result);
        }
        return result;
    }
}

統一資訊包裝類

import lombok.Data;

/**
 * 統一返回資訊包裝類
 *
 * @date 2022/07/15
 */
@Data
public class ResultJson {

    private static final String SUCCESS_CODE = "0000";

    /**
     * 成功失敗的狀態值,true:成功;false:失敗
     * 這裡返回編碼為:“0000”,系統就認為介面成功;其他值,代表失敗
     */
    private Boolean status;

    /**
     * 狀態碼 正確為0000
     */
    private String code;

    /**
     * 返回提示資訊
     */
    private String msg;

    /**
     * 返回資料
     */
    private Object data;

    public ResultJson() {
        this.status = true;
        this.code = SUCCESS_CODE;
        this.msg = "";
    }

    public ResultJson(Object data) {
        this.status = true;
        this.code = SUCCESS_CODE;
        this.msg = "";
        this.data = data;
    }

    public ResultJson(String code, String msg) {
        this.status = SUCCESS_CODE.equals(code);
        this.code = code;
        this.msg = msg;
    }

    /**
     * 如果返回狀態碼非0000,且介面狀態為成功,請使用這個
     * @param status 介面請求狀態
     * @param code 返回code值
     * @param msg 返回訊息
     * @param data 返回資料
     */
    public ResultJson(Boolean status, String code, String msg, Object data) {
        this.status = status;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}

相關文章