封装统一的参数对象和Code工具类

Posted domi22

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了封装统一的参数对象和Code工具类相关的知识,希望对你有一定的参考价值。

一、ResultInfo 实体承载类

package com.test.domi.common.system;

public class ResultInfo<T> {

    private String code;
    private String message;
    private T data;

    public ResultInfo(){
    }

    public  String getCode(){
        return this.code;
    }

    public void setCode(String code){
        this.code = code;
    }

    public  String getMessage(){
        return this.message;
    }

    public void setMessage(String message){
        this.message = message;
    }

    public  Object getData(){
        return this.data;
    }

    public void setData(T data){
        this.data = data;
    }
}

 

二、ResultCode 枚举类

package com.test.domi.common.system;

public enum ResultCode {

    SUCCESS("000000","成功"),
    CONNECT_ERROR("100001","网络连接失败"),
    CONNECT_TIMEOUT("100002","网络连接超时"),
    INTERNAL_SERVER_ERROR("100003","服务器内部错误"),
    QUERY_ERROR("100004","查询失败"),
    INSERT_ERROR("100005","保存数据失败"),
    UPDATE_ERROR("100006","更新数据失败"),
    DELETE_ERROR("100007","删除数据失败");

    private String code;
    private String msg;

    ResultCode(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

 

三、ResultUtil 实例化工具类

package com.test.domi.common.system;

public class ResultUtil {

    public ResultUtil(){
    }

    public static ResultInfo getSuccessResult(Object object){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(ResultCode.SUCCESS.getCode());
        resultInfo.setMessage(ResultCode.SUCCESS.getMsg());
        resultInfo.setData(object);
        return resultInfo;
    }

    public static ResultInfo getFailResult(ResultCode resultCode){
        return getFailResult(resultCode,(Object)null);
    }

    public static ResultInfo getFailResult(String resultCode,String resultMessage,Object data){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(resultCode);
        resultInfo.setMessage(resultMessage);
        resultInfo.setData(data);
        return resultInfo;
    }

    public  static  ResultInfo getFailResult(ResultCode resultCode,Object data){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(resultCode.getCode());
        resultInfo.setMessage(resultCode.getMsg());
        resultInfo.setData(data);
        return resultInfo;
    }

}

 

以上是关于封装统一的参数对象和Code工具类的主要内容,如果未能解决你的问题,请参考以下文章

ef code first 怎样指定哪些类生成数据表?那些不生成数据表?

前后台交互的工具类

轻松把玩HttpClient之封装HttpClient工具类,封装输入参数,简化工具类

轻松把玩HttpClient之封装HttpClient工具类,插件式配置HttpClient对象

轻松把玩HttpClient之封装HttpClient工具类,封装输入参数,简化工具类

JavaScript武器库