工具类(三)返回封装

CommonReturn.java

package com.chinamobile.comm;

import com.chinamobile.constants.ExceptionConstants;

import java.util.HashMap;
import java.util.Map;

/**
 * Description
 *
 * @author <a href="https://qiankunpingtai.cn">qiankunpingtai</a>
 * @Date: 2019/11/19 9:53
 */
public class CommonReturn {
    private Integer code;
    private String msg;
    private Object data;

    public Integer getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
    public  Map<String, Object> getReturn(){
        Map <String, Object> result=new HashMap();
        result.put(ExceptionConstants.GLOBAL_RETURNS_CODE,this.code);
        result.put(ExceptionConstants.GLOBAL_RETURNS_MESSAGE,this.msg);
        result.put(ExceptionConstants.GLOBAL_RETURNS_DATA,this.data);
        return result;
    }
    public static Map<String, Object> createReturn(Integer code,String msg,Object data){
        Map <String, Object> result=new HashMap();
        result.put(ExceptionConstants.GLOBAL_RETURNS_CODE,code);
        result.put(ExceptionConstants.GLOBAL_RETURNS_MESSAGE,msg);
        result.put(ExceptionConstants.GLOBAL_RETURNS_DATA,data);
        return result;
    }
}

ExceptionConstants.java

package com.chinamobile.constants;

/**
 * Description
 *  异常定义常量类
 * @author <a href="https://qiankunpingtai.cn">qiankunpingtai</a>
 * @Date: 2019/6/17 16:38
 */
public class ExceptionConstants {


    public static final String GLOBAL_RETURNS_CODE = "code";
    public static final String GLOBAL_RETURNS_MESSAGE = "msg";
    public static final String GLOBAL_RETURNS_DATA = "data";

    /**
     * 正常返回/操作成功
     **/
    public static final int SERVICE_SUCCESS_CODE = 200;
    public static final String SERVICE_SUCCESS_MSG = "操作成功";
    /**
     * 数据查询异常
     */
    public static final int DATA_READ_FAIL_CODE = 300;
    public static final String DATA_READ_FAIL_MSG = "数据查询异常";
    /**
     * 数据写入异常
     */
    public static final int DATA_WRITE_FAIL_CODE = 301;
    public static final String DATA_WRITE_FAIL_MSG = "数据写入异常";

    /**
     * 系统运行时未知错误
     **/
    public static final int SERVICE_SYSTEM_ERROR_CODE = 500;
    public static final String SERVICE_SYSTEM_ERROR_MSG = "未知异常";
    public static  final  String SYSTEM_IDENTIFICATION_ERROE = "身份认证失败";

    /**
     * 没有查询到任何结果
     * */
    public static final int NOT_ANY_RESULT_FOUND_CODE = 901;
    public static final String NOT_ANY_RESULT_FOUND_MSG = "没有查询到任何结果";
 
}

调用

	@RequestMapping("/test")
	@ResponseBody
	public Map<String, Object> test(HttpServletResponse response, HttpServletRequest request) {
		return CommonReturn.createReturn(ExceptionConstants.SERVICE_SUCCESS_CODE,ExceptionConstants.SERVICE_SUCCESS_MSG,"");
	}
	@RequestMapping("/showtoken")
	@ResponseBody
	public Map<String, Object> showtoken(HttpServletResponse response, HttpServletRequest request) {
		CommonReturn commonReturn=new CommonReturn();
		User u = SessionUtil.getUser(request);
		if (u != null) {
			commonReturn.setCode(ExceptionConstants.SERVICE_SUCCESS_CODE);
			commonReturn.setMsg(ExceptionConstants.SERVICE_SUCCESS_MSG);
			commonReturn.setData(u.getToken());
		} else {
			commonReturn.setCode(ExceptionConstants.LOGIN_NOT_CODE);
			commonReturn.setMsg(ExceptionConstants.LOGIN_NOT_MSG);
			commonReturn.setData("");
		}
		return commonReturn.getReturn();
	}

上一篇 工具类 (二) 操作 sftp