码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-05-27 16:20:59      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:pre   tin   intern   .data   服务器   ltm   col   枚举类   package   

一、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工具类

标签:pre   tin   intern   .data   服务器   ltm   col   枚举类   package   

原文地址:https://www.cnblogs.com/domi22/p/9096247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!