使用RestTemplate接收数据

 使用RestTemplate的postForEntity方法,返回结果为ResponseEntity

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

		 Map params = toParams(deviceInfo); 
		 LocalResponseResult responseResult = super.buildResponseResult(params); //构造返回结果
		 ResponseEntity 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 toParams(DeviceInfo deviceInfo) {
		Map 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;
	}

 

转载于:https://my.oschina.net/u/3761898/blog/1624446

你可能感兴趣的:(使用RestTemplate接收数据)