Java:返回结果统一转换

返回结果统一转换

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.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 返回结果统一转换
 */
@RestControllerAdvice(basePackages = "com.wuxian")
public class ResponseAdvisor implements ResponseBodyAdvice<Object> {

    /**
     *  判断哪些需要拦截
     */
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    /**
     * 返回结果包装
     * @param body controller返回的数据
     * @param returnType
     * @param selectedContentType
     * @param selectedConverterType
     * @param request
     * @param response
     * @return
     */
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {

        if (body instanceof Result){
            return body;
        }

        if (body instanceof Boolean){
            boolean result = (boolean)body;
            return new BaseResponse<Boolean>(result);
        }

        if (body instanceof PageVO){
            return new BaseResponse<>(body);
        }

        if (body instanceof ExceptionResponse){
            return new BaseResponse<>(400,((ExceptionResponse)body).getMsg());
        }

        return new BaseResponse<>(body);
    }
}

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 返回结果通用对象封装
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BaseResponse<D> implements Serializable {

    private static final long serialVersionUID = -2145852753254617071L;

    //是否成功
    private boolean success;

    //异常信息
    private String message;

    //200表示正常,300接口返回false,400调用异常
    private int code;

    //泛型,用来存储原有controller返回值
    private D data;

    public BaseResponse() {
        this.code = 200;
        this.message = "success";
        this.success = true;
    }


    public BaseResponse(D data){
        this();
        this.data = data;
    }

    public BaseResponse(boolean success){
        this();
        this.success = success;
        if(success == false){
            this.message = "操作失败";
            this.code = 300;
        }
    }

    public BaseResponse(boolean success,String msg){
        this(success);
        this.message = msg;
    }

    public BaseResponse(int code,String msg){
        this(false,msg);
        this.code = code;
    }
}

/**
 * 返回结果通用封装
 */
@Data
public class Result {
    // 返回状态
    private int status;
    // 状态描述
    private String desc;
    // 返回数据
    private Object data;

    private String token;
}
@Data
public class ExceptionResponse{
    private String msg;
    public ExceptionResponse(String msg){
        this.msg = msg;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员无羡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值