All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.com.antcloud.api.common.BaseClientResponse Maven / Gradle / Ivy

Go to download

Ant Fin Tech API SDK For Java Copyright (c) 2015-present Alipay.com, https://www.alipay.com

The newest version!
/*
 * Copyright (c) 2015-present Alipay.com, https://www.alipay.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.com.antcloud.api.common;

import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Type;

/**
 * BaseClientResponse
 */
public class BaseClientResponse {

    private JSONObject data;

    /**
     * 返回成功值
     * @param t
     * @param o
     * @param 
     * @return
     */
    @Deprecated
    protected static  T success(T t, Object o) {
        SDKUtils.checkNotNull(o);

        JSONObject jsonObject = GwJsons.toJSON(o);
        jsonObject.put(SDKConstants.ParamKeys.RESULT_CODE, SDKConstants.ResultCodes.OK);

        t.setData(jsonObject);

        return t;
    }

    /**
     * 返回成功值
     * @param t
     * @param request
     * @param o
     * @param 
     * @return
     */
    protected static  T success(T t, BaseClientRequest request,
                                                              Object o) {
        SDKUtils.checkNotNull(o);

        JSONObject jsonObject = GwJsons.toJSON(o);
        jsonObject.put(SDKConstants.ParamKeys.RESULT_CODE, SDKConstants.ResultCodes.OK);
        jsonObject.put(SDKConstants.ParamKeys.REQ_MSG_ID, request.getReqMsgId());

        t.setData(jsonObject);

        return t;
    }

    /**
     * 返回错误
     * @param t
     * @param resultCode
     * @param resultMsg
     * @param 
     * @return
     */
    @Deprecated
    public static  T error(T t, String resultCode, String resultMsg) {
        SDKUtils.checkNotNull(resultCode);
        SDKUtils.checkNotNull(resultMsg);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(SDKConstants.ParamKeys.RESULT_CODE, resultCode);
        jsonObject.put(SDKConstants.ParamKeys.RESULT_MSG, resultMsg);

        t.setData(jsonObject);

        return t;
    }

    /**
     * 返回错误
     * @param t
     * @param request
     * @param resultCode
     * @param resultMsg
     * @param 
     * @return
     */
    public static  T error(T t, BaseClientRequest request,
                                                         String resultCode, String resultMsg) {
        SDKUtils.checkNotNull(resultCode);
        SDKUtils.checkNotNull(resultMsg);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(SDKConstants.ParamKeys.RESULT_CODE, resultCode);
        jsonObject.put(SDKConstants.ParamKeys.RESULT_MSG, resultMsg);
        jsonObject.put(SDKConstants.ParamKeys.REQ_MSG_ID, request.getReqMsgId());

        t.setData(jsonObject);

        return t;
    }

    /**
     * 判断是否成功
     * @return
     */
    public boolean isSuccess() {
        return SDKConstants.ResultCodes.OK.equals(getResultCode());
    }

    /**
     * 获取req_msg_id
     * @return
     */
    public String getReqMsgId() {
        return data.getString(SDKConstants.ParamKeys.REQ_MSG_ID);
    }

    /**
     * set req_msg_id
     * @param reqMsgId
     */
    public void setReqMsgId(String reqMsgId) {
        data.put(SDKConstants.ParamKeys.REQ_MSG_ID, reqMsgId);
    }

    /**
     * get result code
     * @return
     */
    public String getResultCode() {
        return data.getString(SDKConstants.ParamKeys.RESULT_CODE);
    }

    /**
     * get result msg
     * @return
     */
    public String getResultMsg() {
        return data.getString(SDKConstants.ParamKeys.RESULT_MSG);
    }

    /**
     * get data
     * @return
     */
    public String getData() {
        return GwJsons.toString(data);
    }

    /**
     * set data
     * @param data
     */
    public void setData(String data) {
        this.data = GwJsons.parse(data);
    }

    /**
     * set data
     * @param data
     */
    public void setData(JSONObject data) {
        this.data = data;
    }

    /**
     * get data
     * @param clazz
     * @param 
     * @return
     */
    public  T getData(Class clazz) {
        return GwJsons.parse(data, clazz);
    }

    /**
     * get data
     * @param type
     * @param 
     * @return
     */
    public  T getData(Type type) {
        return GwJsons.parse(data, type);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy