springboot返回结果包装统一返回格式

 

统一返回结果拦截处理类

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;
    }
}

 

posted @ 2024-09-07 16:39  程序员小艺  阅读(37)  评论(0编辑  收藏  举报