高复用性响应对象类

Posted zhangblearn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高复用性响应对象类相关的知识,希望对你有一定的参考价值。

ServerResponse响应类:
import com.cxsz.ibosspro.common.constant.ResultCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

//保证序列化的时候,如果为null,key也会消失
@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
public class ServerResponse<T> implements Serializable {
    private int status;
    private String msg;
    private T data;

    public ServerResponse() {
    }

    public ServerResponse(int status){
        this.status = status;
    }
    public ServerResponse(int status, String msg){
        this.status = status;
        this.msg = msg;
    }
    public ServerResponse(int status, T data){
        this.status = status;
        this.data = data;
    }
    public ServerResponse(int status, String msg, T data){
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    //使之不在json序列化结果当中
    @JsonIgnore
    public boolean isSuccess(){
        return this.status == ResultCode.SUCCESS;
    }
    public int getStatus(){
        return status;
    }
    public T getData(){
        return data;
    }
    public String getMsg(){
        return msg;
    }

    public static <T> ServerResponse<T> createBySuccess(){
        return new ServerResponse<T>(ResultCode.SUCCESS);
    }
    public static <T> ServerResponse<T> createBySuccessMassage(String msg){
        return new ServerResponse<T>(ResultCode.SUCCESS, msg);
    }
    public static <T> ServerResponse<T> createBySuccess(T data){
        return new ServerResponse<T>(ResultCode.SUCCESS, data);
    }
    public static <T> ServerResponse<T> createBySuccess(String msg, T data){
        return new ServerResponse<T>(ResultCode.SUCCESS, msg, data);
    }
    public static <T> ServerResponse<T> createByError(){
        return new ServerResponse<T>(ResultCode.FAIL);
    }
    public static <T> ServerResponse<T> createByError(String msg){
        return new ServerResponse<T>(ResultCode.FAIL, msg);
    }
    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorcode, String msg){
        return new ServerResponse<T>(errorcode, msg);
    }
}

 

常量类:
/**
 * HTTP请求返回状态码
 *
 * @author xxxx
 */
public class ResultCode {

    /**
     * 成功
     */
    public static final int SUCCESS = 0;

    /**
     * 失败
     */
    public static final int FAIL = -1;

    /**
     * 无权限
     */
    public static final int NO_AUTH = -2;
}

 













以上是关于高复用性响应对象类的主要内容,如果未能解决你的问题,请参考以下文章

高复用服务响应对象的设计思想以及抽象封装

CAPL 封装了的SeedKey解锁函数,高复用性

CAPL 封装了的SeedKey解锁函数,高复用性

CAPL 封装了的SeedKey解锁函数,高复用性

利用Jackson封装常用JsonUtil工具类

UED团队规范设计参考及建议