您的位置:首页 > 其它

使用RestTemplate接收数据

2018-02-26 00:00 411 查看
使用RestTemplate的postForEntity方法,返回结果为ResponseEntity<T>

/**
* 增加设备
*
* @param deviceInfo
* @return
*/
@RequestMapping(path = "/add", method = RequestMethod.POST)
public ResponseResult addDevice(DeviceInfo deviceInfo) {
super.checkLoginUserAuth();//权限验证相关方法

Map<String, Object> params = toParams(deviceInfo);
LocalResponseResult responseResult = super.buildResponseResult(params); //构造返回结果
ResponseEntity<Map> response = this.restTemplate.postForEntity("/device", params, Map.class);

if (response.getStatusCode() != HttpStatus.OK) {
return responseResult.result(response.getStatusCode());
}
return responseResult.result(HttpStatus.OK, response.getBody());
}

/**
* 将注册对象转化为参数Map
*
* @param deviceInfo
* @return
*/
private Map<String, Object> toParams(DeviceInfo deviceInfo) {
Map<String, Object> params = Maps.newLinkedHashMap();
params.put("uuid", UUID.randomUUID().toString());
params.put("depotId", deviceInfo.getDepotId());
params.put("deviceSerialNumber", deviceInfo.getDeviceSerialNumber());

String direction = null;
if (deviceInfo.getDirection() == Direction.IN) {
direction = "IN";
} else if (deviceInfo.getDirection() == Direction.OUT) {
direction = "OUT";
}
params.put("direction", direction);
params.put("operatorId", deviceInfo.getOperatorId());
params.put("operatorName", deviceInfo.getOperatorName());
params.put("remark", deviceInfo.getRemark());
return params;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  RestTemplate