消息响应主体 ResponseData 工具类

前言

        在系统开发的过程中,我们会尝尝遇到数据返回不统一、散、且乱的问题,尤其是前后端分离的情况下,这个问题就更明显了,怎么样来解决这个问题呢,这时就需要我们抽象出一个公共的、一致的、标准的数据载体(数据响应实体),以下为一个实例,当然需要更具实际的具体业务需求进行调整的。

ResponseData 工具类实例

1、配置默认构造函数

	/**
	 * 默认构造函数
	 */
	public ResponseData()
	{

		put("code", RespCodeEnum.SUCCESS.getCode());
		put("msg", "success");
	}

2、系统正常并返回数据

/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title succee
	 *        <ul>
	 * @description
	 *              <li>系统正常并返回数据
	 *              </ul>
	 * @createdTime 2018年4月17日 下午4:51:41
	 * @param map
	 *            响应数据
	 * @return ResponseData 响应主体
	 *
	 * @version : V1.0.0
	 */
	public static ResponseData succee(final Map<String, Object> map)
	{
		ResponseData responseData = new ResponseData();

		responseData.putAll(map);
		return responseData;
	}

3、系统正常并返回对应消息

/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title succee
	 *        <ul>
	 * @description
	 *              <li>系统正常并返回对应消息
	 *              </ul>
	 * @createdTime 2018年4月17日 下午4:52:44
	 * @param msg
	 *            响应消息
	 * @return ResponseData 响应主体
	 *
	 * @version : V1.0.0
	 */
	public static ResponseData succee(final String msg)
	{
		ResponseData responseData = new ResponseData();

		responseData.put("msg", msg);
		return responseData;
	}

4、系统异常

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title failed
	 *        <ul>
	 * @description
	 *              <li>系统异常
	 *              </ul>
	 * @createdTime 2018年4月17日 下午4:47:03
	 * @param msg
	 *            异常信息
	 * @return ResponseData 响应主体
	 *
	 * @version : V1.0.0
	 */
	public static ResponseData failed(final String msg)
	{

		return failed(RespCodeEnum.ERROR_INTERNAL_SERVER.getCode(), msg);
	}

5、系统异常

/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title failed
	 *        <ul>
	 * @description
	 *              <li>系统异常
	 *              </ul>
	 * @createdTime 2018年4月17日 下午4:48:34
	 * @param code
	 *            异常状态码
	 * @param msg
	 *            异常信息
	 * @return ResponseData 响应主体
	 *
	 * @version : V1.0.0
	 */
	public static ResponseData failed(final int code, final String msg)
	{
		ResponseData responseData = new ResponseData();

		responseData.put("code", code);
		responseData.put("msg", msg);
		return responseData;
	}

6、设置响应数据体,可以同时设置多个

    /**
	 * 设置响应数据体,可以同时设置多个
	 */
	@Override
	public ResponseData put(final String key, final Object value)
	{
		super.put(key, value);
		// 当前实例
		return this;
	}

7、完整内容示例

package com.huazai.b2c.aiyou.controller.tools.common;

import com.huazai.b2c.aiyou.controller.tools.common.enums.RespCodeEnum;

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

/**
 * 
 * @author HuaZai
 * @contact who.seek.me@java98k.vip
 *          <ul>
 * @description
 *              <li>消息响应主体类
 *              </ul>
 * @className ResponseData
 * @package com.huazai.b2c.aiyou.controller
 * @createdTime 2018年4月17日 下午4:37:59
 *
 * @version V1.0.0
 */
public class ResponseData extends HashMap<String, Object>
{

	/**
	 * 序列化ID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 默认构造函数
	 */
	public ResponseData()
	{

		put("code", RespCodeEnum.SUCCESS.getCode());
		put("msg", "success");
	}

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title succee
	 *        <ul>
	 * @description
	 *              <li>系统正常
	 *              </ul>
	 * @createdTime 2018年4月17日 下午4:50:01
	 * @return ResponseData 响应主体
	 *
	 * @version : V1.0.0
	 */
	public static ResponseData succee()
	{

		return new ResponseData();
	}

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title succee
	 *        <ul>
	 * @description
	 *              <li>系统正常并返回数据
	 *              </ul>
	 * @createdTime 2018年4月17日 下午4:51:41
	 * @param map
	 *            响应数据
	 * @return ResponseData 响应主体
	 *
	 * @version : V1.0.0
	 */
	public static ResponseData succee(final Map<String, Object> map)
	{
		ResponseData responseData = new ResponseData();

		responseData.putAll(map);
		return responseData;
	}

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title succee
	 *        <ul>
	 * @description
	 *              <li>系统正常并返回对应消息
	 *              </ul>
	 * @createdTime 2018年4月17日 下午4:52:44
	 * @param msg
	 *            响应消息
	 * @return ResponseData 响应主体
	 *
	 * @version : V1.0.0
	 */
	public static ResponseData succee(final String msg)
	{
		ResponseData responseData = new ResponseData();

		responseData.put("msg", msg);
		return responseData;
	}

	/**
	 * 设置响应数据体,可以同时设置多个
	 */
	@Override
	public ResponseData put(final String key, final Object value)
	{
		super.put(key, value);
		// 当前实例
		return this;
	}

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title failed
	 *        <ul>
	 * @description
	 *              <li>未知的系统异常
	 *              </ul>
	 * @createdTime 2018年4月17日 下午4:46:18
	 * @return ResponseData 响应的主体
	 *
	 * @version : V1.0.0
	 */
	public static ResponseData failed()
	{

		return failed(RespCodeEnum.ERROR_INTERNAL_SERVER.getCode(), "未知的系统异常,请联系运维人员");
	}

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title failed
	 *        <ul>
	 * @description
	 *              <li>系统异常
	 *              </ul>
	 * @createdTime 2018年4月17日 下午4:47:03
	 * @param msg
	 *            异常信息
	 * @return ResponseData 响应主体
	 *
	 * @version : V1.0.0
	 */
	public static ResponseData failed(final String msg)
	{

		return failed(RespCodeEnum.ERROR_INTERNAL_SERVER.getCode(), msg);
	}

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title failed
	 *        <ul>
	 * @description
	 *              <li>系统异常
	 *              </ul>
	 * @createdTime 2018年4月17日 下午4:48:34
	 * @param code
	 *            异常状态码
	 * @param msg
	 *            异常信息
	 * @return ResponseData 响应主体
	 *
	 * @version : V1.0.0
	 */
	public static ResponseData failed(final int code, final String msg)
	{
		ResponseData responseData = new ResponseData();

		responseData.put("code", code);
		responseData.put("msg", msg);
		return responseData;
	}

}


 好了,关于 消息响应主体 ResponseData 工具类 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。 
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。


作       者:华    仔
联系作者:who.seek.me@java98k.vip
来        源:CSDN (Chinese Software Developer Network)
原        文:https://blog.csdn.net/Hello_World_QWP/article/details/89360521
版权声明:本文为博主原创文章,请在转载时务必注明博文出处!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值