responseresult.java_ResponseResult通用数据的传输对象工具类

本文介绍了一种通用的数据传输对象ResponseResult的设计方案及其状态码定义。该对象用于封装API调用的结果,包括状态码、消息及返回数据。适用于前后端交互场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**

* 通用数据传输对象

*

* Description:

*

*

* @author Songzixian

* @version v1.0.0

* @date 2019-07-26 04:43:54

* @see com.songzixian.commons.dto

*/

@Data

public class ResponseResult implements Serializable {

private static final long serialVersionUID = -3999803560577989187L;

/**

* 状态码

*/

private Integer code;

/**

* 消息

*/

private String message;

/**

* 返回对象

*/

private T data;

public ResponseResult() {

super();

}

public ResponseResult(Integer code) {

super();

this.code = code;

}

public ResponseResult(Integer code, String message) {

super();

this.code = code;

this.message = message;

}

public ResponseResult(Integer code, Throwable throwable) {

super();

this.code = code;

this.message = throwable.getMessage();

}

public ResponseResult(Integer code, T data) {

super();

this.code = code;

this.data = data;

}

public ResponseResult(Integer code, String message, T data) {

super();

this.code = code;

this.message = message;

this.data = data;

}

public T getData() {

return data;

}

public void setData(T data) {

this.data = data;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((data == null) ? 0 : data.hashCode());

result = prime * result + ((message == null) ? 0 : message.hashCode());

result = prime * result + ((code == null) ? 0 : code.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj) {

return true;

}

if (obj == null) {

return false;

}

if (getClass() != obj.getClass()) {

return false;

}

ResponseResult> other = (ResponseResult>) obj;

if (data == null) {

if (other.data != null) {

return false;

}

} else if (!data.equals(other.data)) {

return false;

}

if (message == null) {

if (other.message != null) {

return false;

}

} else if (!message.equals(other.message)) {

return false;

}

if (code == null) {

if (other.code != null) {

return false;

}

} else if (!code.equals(other.code)) {

return false;

}

return true;

}

/**

* 通用状态码

*

* Description:

*

*

* @author Lusifer

* @version v1.0.0

* @date 2019-07-30 05:02:49

* @see

*/

public class CodeStatus {

/**

* 请求成功

*/

public static final int OK = 20000;

/**

* 请求失败

*/

public static final int FAIL = 20002;

/**

* 熔断请求

*/

public static final int BREAKING = 20004;

/**

* 非法请求

*/

public static final int ILLEGAL_REQUEST = 50000;

/**

* 非法令牌

*/

public static final int ILLEGAL_TOKEN = 50008;

/**

* 其他客户登录

*/

public static final int OTHER_CLIENTS_LOGGED_IN = 50012;

/**

* 令牌已过期

*/

public static final int TOKEN_EXPIRED = 50014;

}

}

public class GatewayStub implements IDeviceBridge { private static final Logger LOG = LoggerFactory.getLogger(GatewayStub.class); private IBridgeContext context; private ScheduledExecutorService schduler = Executors.newScheduledThreadPool(1); private Random random = new Random(); private SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static List<String> msList = new ArrayList<>(); @Override public String bridgeType() { // TODO Auto-generated method stub return "gatewayStub"; } @Override public void init(AgentConfig config, IBridgeContext context) { try { this.context = context; LOG.info("init into"); //LogPushUdpClient logPushUdpClient = new LogPushUdpClient(); // logPushUdpClient.run(); }catch (Exception e){ System.out.println(e); } } @Override public void onDeviceCommand(DeviceInfo device, String serviceId, String cmdName, JSONObject cmdProperties, ServiceCallback<DeviceCommandResponse> callback) { // TODO Auto-generated method stub LOG.info("Received command, serviceId:{}, cmdName:{}, cmdProperties:{}", serviceId, cmdName, cmdProperties.toJSONString()); JSONObject result = new JSONObject(); result.put("requestParam", cmdProperties); result.put("state", "ok"); result.put("time", System.currentTimeMillis()); DeviceCommandResponse resp = new DeviceCommandResponse(); resp.setDeviceIdentifier(device.getDeviceIdentifier()); resp.setResult(ResponseResult.success); resp.setMessage("Cmd success"); resp.setCmdResponse(result); callback.onResponse(resp); } @Override public void onDeviceDelete(DeviceInfo info) { // TODO Auto-generated method stub } @Override public void start() { DeviceInfo childDevice = new DeviceInfo(null, "test_root", "Terminal", "12334445", "test_root"); childDevice.setDeviceIdentifier("jichao_001"); try{ context.registerDevice(childDevice, resp -> { if (resp.getResult() == ResponseResult.fail) { LOG.error("Failed to register device:{}", resp.getMessage()); return; } DeviceStatus deviceStatus = new DeviceStatus(); deviceStatus.setDeviceIdentifier("jichao_001"); deviceStatus.setConnectStatus(DeviceConnStatus.online); context.changeDeviceConnectStatus(deviceStatus, onlineResp -> { if (onlineResp.getResult() == ResponseResult.fail) { LOG.error("Failed to change device state:{}", resp.getMessage()); return; } schduler.scheduleAtFixedRate(() -> { JSONObject report = new JSONObject(); report.put("random", random.nextInt()); report.put("reportTime", dateFormater.format(new Date())); context.propertyReport(childDevice, "testService", report); }, 1, 10, TimeUnit.SECONDS); }); }); }catch (Exception e){ LOG.error("Failed to register or changeDeviceConnectStatus device:{}", childDevice.getDeviceIdentifier()); } } @Override public void stop() { // TODO Auto-generated method stub } }
最新发布
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值