问题描述
创建实体时,您可以这样做:
curl --location --request POST 'https://my.api/v2/entities/?options=keyvalues' --header 'Content-Type: application/json' --data-raw '{
"id":"vehicle:Wastemanagement:1","type":"Wastemanagement","vehicleType":"lorry"
}'
或者没有 options=keyvalues 参数:
curl --location --request POST 'https://my.api/v2/entities/' --header 'Content-Type: application/json' --data-raw '{
"id":"vehicle:Wastemanagement:2","vehicleType":{
"value": "lorry"
}
}'
在这两种情况下,实体都是相同的。 但我没有设法创建一个带有 options=keyvalues 参数的实体,该实体具有一个具有“位置”属性的实体:在做时
curl --location --request POST 'https://my.api/v2/entities/' --header 'Content-Type: application/json' --data-raw '{
"id":"vehicle:Wastemanagement:3","vehicleType":"lorry","location": {
"type": "Point","coordinates": [
-0.56832066,47.49576
]
}
}'
在这种情况下, curl --location --request GET https://my.api/v2/entities/vehicle:WasteManagement:2' 的结果
会
"location": {
"type": "StructuredValue","value": {
"type": "Point","coordinates": [
-3.164485592,40.627851337
]
},"Metadata": {}
},
位置类型为StructuredValue
我的期望是 geo:point
或其他 geo:XXX,具体取决于 GeoJSON 的类型,这允许实体进行地理定位并按位置过滤。
解决方法
根据 NGSIv2 specification 部分“部分陈述”:
属性/元数据 type
可以在请求中省略。在属性/元数据创建或更新操作中省略时,根据值对类型使用默认值:
- ...
- 如果 value 是对象或数组,则使用
StructuredValue
。
在要求的情况下
curl --location --request POST 'https://my.api/v2/entities/?options=keyValue' --header 'Content-Type: application/json' --data-raw '{
"id":"vehicle:WasteManagement:3","type":"WasteManagement","vehicleType":"lorry","location": {
"type": "Point","coordinates": [
-0.56832066,47.49576
]
}
}'
location
属性满足 NGSIv2 规范的“当 [属性类型] 在属性/元数据创建或更新操作中省略时”。此外,value 是一个对象,因此 “如果 value 是一个对象或数组,则使用 StructuredValue
”。因此,使用 StructuredValue
类型创建/更新属性是正确的。
解决方法非常简单:如果您需要具有特殊类型的属性(如位置或 DateTime
的属性),请不要使用 keyValues 模式,而是使用规范化模式。
请注意,keyValues 模式被设计为帮助器,而不是作为规范化模式的替代品。 keyValues 模式涵盖了很大程度的规范化模式功能,但并不完全。