自定义接口返回统一格式

自定义接口返回统一格式

package com.risen.demo.util;

import com.risen.hp.fastjson.JSONObject;
import org.springframework.http.HttpStatus;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @ClassName: ResponseUtil
 * @Description: 自定义接口返回统一格式
 * @Author: fxb
 * @Date: 2019/9/1 15:39
 * @Version V1.0
 **/
public class ResponseTemplate {

    public static final String MSG_META = "meta";
    public static final String MSG_META_RESP_CODE = "respCode";
    public static final String MSG_META_RESP_MSG = "respMsg";
    public static final String MSG_META_SERVICE_CODE = "serviceCode";
    public static final String MSG_META_RESP_VER = "ver";
    public static final String MSG_META_RESP_VERSION = "01";
    public static final String RESULT_DATA = "data";

    /**
     *  定义返回结构存储为json
     *  */
    private JSONObject resultJson = new JSONObject();

    /**
     * 定义 状态信息存储集合
     * */
    private Map<String ,Object> meta = new LinkedHashMap<String ,Object>();

    /**
     *  定义 返回结构数据存储集合
     *  */
    private Object data = new LinkedList<Object>();

    /**
     * 外部静止实例化
     */
    protected ResponseTemplate() {

    }

    protected ResponseTemplate(HttpStatus status) {
        init(status);
    }

    /**
     *  向外提供一个创建默认 请求成功的实例入口
     * @return ResponseUtil
     */
    public static ResponseTemplate createOk(){
        return new ResponseTemplate(HttpStatus.OK);
    }

    /**
     *  向外提供一个创建默认 请求失败的实例入口
     * @return ResponseUtil
     */
    public static ResponseTemplate createFail(){
        return new ResponseTemplate(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 返回结构数据
     */
    public JSONObject getResultJson(){
        resultJson.put(MSG_META,meta);
        resultJson.put(RESULT_DATA,data);
        return this.resultJson;
    }

    /**
     * 再返回实例前 先初始化一些内部数据
     * */
    private void init(HttpStatus status){
        meta.put(MSG_META_RESP_CODE, String.valueOf(status.value()));
        meta.put(MSG_META_RESP_MSG,status);
        meta.put(MSG_META_SERVICE_CODE,"");
        meta.put(MSG_META_RESP_VER,MSG_META_RESP_VERSION);
    }

    public void setServerError(String value){
        meta.put(MSG_META_RESP_CODE,String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR));
        meta.put(MSG_META_RESP_MSG,String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase() + value));
    }

    public void setBadRequest(String value){
        meta.put(MSG_META_RESP_CODE,String.valueOf(HttpStatus.BAD_REQUEST));
        meta.put(MSG_META_RESP_MSG,String.valueOf(HttpStatus.BAD_REQUEST.getReasonPhrase() + value));
    }

    public void setMsgMetaRespCode(String value){
        meta.put(MSG_META_RESP_CODE, value);
    }
    public String getMsgMetaRespCode(String value){
        return (String) meta.get(MSG_META_RESP_CODE);
    }

    public void setMsgMetaRespMsg(String value){
         meta.put(MSG_META_RESP_MSG,value);
    }
    public String getMsgMetaRespMsg(){
        return  (String) meta.get(MSG_META_RESP_MSG);
    }

    public void setMsgMetaServiceCode(String value){
        meta.put(MSG_META_SERVICE_CODE,value);
    }
    public String getMsgMetaServiceCode(){
        return (String) meta.get(MSG_META_SERVICE_CODE);
    }

    public void setMsgMetaRespVer(String value){
        meta.put(MSG_META_RESP_VER,value);
    }
    public String getMsgMetaRespVer(){
        return (String) meta.get(MSG_META_RESP_VER);
    }

    public void setResultData(Object valueData){
        this.data = valueData;
    }
    public Object getResultData(){
        return this.data;
    }

}

发布了33 篇原创文章 · 获赞 2 · 访问量 4750

猜你喜欢

转载自blog.csdn.net/qq_36778310/article/details/102491924