统一接口数据返回格式

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_35033270/article/details/79998661

为了便于对接,数据返回的格式最好一致,包括返回的状态,提示信息,和需要的数据,则将其封装为统一格式,如下:

public class ResultData<T> {
    /**
     * 结果状态
     */
    private boolean status;
    /**
     * 结果数据信息
     */
    private T data;
    /**
     * 结果提示信息
     */
    private String msg;


    public ResultData() {
        super();
    }
    public ResultData(boolean status, T data, String msg) {
        super();
        this.status = status;
        this.data = data;
        this.msg = msg;
    }
    /**
     * @return the status
     */
    public boolean isStatus() {
        return status;
    }
    /**
     * @param status the status to set
     */
    public void setStatus(boolean status) {
        this.status = status;
    }
    /**
     * @return the data
     */
    public T getData() {
        return data;
    }
    /**
     * @param data the data to set
     */
    public void setData(T data) {
        this.data = data;
    }
    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }
    /**
     * @param msg the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "{"+"\""+"status"+"\""+":" + status + ", "+"\""+"data"+"\""+":" + data + ","+"\""+ "msg"+"\"" +":" +"\""+ msg +"\"" + "}";
    }
}

则使用的的时候就很简单了,如下:

public String updateSensitiveWord(String sensitiveWord,String oldsensitiveWord){
        ResultData<String> resData = new ResultData<String>();
        if(StringUtils.isEmpty(sensitiveWord) || StringUtils.isEmpty(oldsensitiveWord)){
            resData.setStatus(false);
            resData.setMsg("更新异常,传入的原敏感词或更新敏感词为空!");
            return resData.toString();
        }
        if(sensitiveWord.equals(oldsensitiveWord)){
            logger.info("更新的敏感词和原有敏感词一致");
            resData.setStatus(false);
            resData.setMsg("更新异常,更新的敏感词和原有敏感词一致");
            return resData.toString();
        }
        try {
            SensitiveWord sensitiveWords =new SensitiveWord();
            sensitiveWords.setIsDeleted(0);
            sensitiveWords.setWord(sensitiveWord);
            List<SensitiveWord> sensitiveWores = sensitiveWordMapper
                    .select(sensitiveWords);
            if (CollectionUtils.isEmpty(sensitiveWores)) {
                sensitiveWordMapper.updateSensitiveWord(sensitiveWord,
                        oldsensitiveWord);
                // 更新缓存敏感词
                sensitiveWordCache.updateSensitiveWord(sensitiveWord,
                        oldsensitiveWord);
                resData.setStatus(true);
                resData.setMsg("更新敏感词成功!");
            } else {
                resData.setStatus(false);
                resData.setMsg("传入的新敏感词已经存在!");
            }
        } catch (Throwable e) {
            logger.info("更新敏感词异常:"+e);
            resData.setStatus(false);
            resData.setMsg("更新敏感词异常,请检查数据库服务是否正常");
         }
        return resData.toString();
        }
展开阅读全文

没有更多推荐了,返回首页