zoukankan      html  css  js  c++  java
  • 统一封装json返回结果

    import org.codehaus.jackson.annotate.JsonIgnore;
    import org.codehaus.jackson.map.annotate.JsonSerialize;
    
    import java.io.Serializable;
    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
    //保证序列化json的时候,如果是null的对象,key也会消失
    public class ServiceResponse<T> implements Serializable {
        private int status;  //结果状态值
        private String msg;  //结果信息
        private T data;      //结果数据
    
        private ServiceResponse(int status){
            this.status = status;
        }
    
        private ServiceResponse(int status, T data){
            this.status = status;
            this.data = data;
        }
    
        private ServiceResponse(int status, String msg, T data){
            this.status = status;
            this.msg = msg;
            this.data = data;
        }
    
        private ServiceResponse(int status, String msg){
            this.status = status;
            this.msg = msg;
        }
    
        @JsonIgnore //让其不在json序列化中
        public boolean isSuccess(){
            return this.status == ResponseCode.SUCCESS.getCode();
        }
    
        public int getStatus() {
            return status;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public T getData() {
            return data;
        }
    
        public static <T> ServiceResponse<T> createBySuccess(){
            return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode());
        }
    
        public static <T> ServiceResponse<T> createBySuccessMessage(String msg){
            return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), msg);
        }
    
        public static <T> ServiceResponse<T> createBySuccess(T data){
            return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), data);
        }
    
        public static <T> ServiceResponse<T> createBySuccess(String msg, T data){
            return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data);
        }
    
        public static <T> ServiceResponse<T> createByError(){
            return new ServiceResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
        }
    
        public static <T> ServiceResponse<T> createByErrorMessage(String errorMessage){
            return new ServiceResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
        }
    
        public static <T> ServiceResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage){
            return new ServiceResponse<T>(errorCode, errorMessage);
        }
    }

     使用到的枚举值

    /**
     * 状态枚举值
     */
    public enum ResponseCode {
    
        SUCCESS(0,"SUCCESS"),
        ERROR(1,"ERROR"),
        NEED_LOGIN(10,"NEED_LOGIN"),
        ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT");
    
        private final int code;
        private final String desc;
    
        ResponseCode(int code, String desc){
            this.code = code;
            this.desc = desc;
        }
        public int getCode() {
            return code;
        }
        public String getDesc() {
            return desc;
        }
    }
  • 相关阅读:
    Oracle DUL/AUL/ODU 工具说明
    Mysql 不同版本 说明
    Oracle RAC ASM disk header 备份 恢复 与 重建 示例说明
    Oracle checkpoint 说明
    Oracle RBA(Redo Byte Address) 说明
    Oracle wrap 和 unwrap( 加密与解密) 说明
    Oracle 10.2.0.5.4 Patch Set Update (PSU) – Patch No: p12419392
    Oracle RAC CSS 超时计算 及 参数 misscount, Disktimeout 说明
    Oracle 估算数据库大小的方法
    Oracle 11g 11.2.0.2 Bug 10082277 – Excessive allocation in PCUR or KGLH0 heap of “kkscsAddChildNo”
  • 原文地址:https://www.cnblogs.com/FondWang/p/12050879.html
Copyright © 2011-2022 走看看