Thingsboard 3.1.0 - 规则链:外部结点REST API

调用REST API Call到外部REST服务器。
在这里插入图片描述

一、REST API参数说明

  1. Endpoint URL pattern
    可以是静态字符串,也可以是Message Metadata的属性。

例如,如果消息负载中包含带有属性deviceType值为container,

http://localhost/api/${deviceType}/update

会被解析为:

http://localhost/api/container/update
  1. Request method
    GET,POST,PUT,DELETE

  2. Headers
    request headers, header or value 可以是静态字符串,也可以是Message Metadata的属性。
    可以配置header的名称/值。可以是静态字符串,也可以是Message Metadata的属性。例如$ {deviceType}。

  3. Request body
    会把完整的消息负载发送给设置的REST服务器。如果需要,可以将规则链配置为使用转换节点后,发送转换节点后的消息负载。

  4. Outbound message
    出结点消息包含从Rest服务返回的status,statusCode,statusReason和headers做为Message Metadata。消息负载是Rest服务器返回的response body。
    如果请求成功,出结点消息将通过Success链传递到下一个节点,否则将使用Failure链。

二、实际实现

  1. 使用Spring的controller,作为服务端的测试接口。
@RestController
@RequestMapping("/")
public class DataController {
	private static final Logger logger = Logger.getLogger(DataController.class);

	@RequestMapping(value = "/pushdata")
	public Object pushdata(HttpServletRequest request) throws Exception {
		String json = getRequestJsonString(request);
		logger.info("json=" + json);
		
		return "OK";
	}

	/***
	 * 获取 request 中 json 字符串的内容
	 *
	 * @param request
	 * @return : String 
	 * @throws IOException
	 */
	public static String getRequestPostBytes(HttpServletRequest request) throws IOException {
		int contentLength = request.getContentLength();
		if (contentLength < 0) {
			return null;
		}
		byte buffer[] = new byte[contentLength];
		for (int i = 0; i < contentLength;) {
			int readlen = request.getInputStream().read(buffer, i, contentLength - i);
			if (readlen == -1) {
				break;
			}
			i += readlen;
		}
		String charEncoding = request.getCharacterEncoding();
		if (charEncoding == null) {
			charEncoding = "UTF-8";
		}
		return new String(buffer, charEncoding);
	}
}
  1. 规则链设置
    在这里插入图片描述

  2. 实际效果
    实际LOG输出:

2020-10-01 13:14:52.520 INFO  c.controller.DataController - json={
  "M.TAG.D001" : 2670,
  "M.TAG.D002" : 3370,
  "M.TAG.D003" : 6400,
  "M.TAG.D004" : 8450,
  "M.TAG.D005" : 4500,
  "M.TAG.D006" : 1355.88,
  "M.TAG.D007" : 3082
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值