[英]How can I send a JsonObject request, and recieve a NodeResponse in the MeshRestClient?
在我的 Gentics Mesh 插件中,我從 json 文件創建節點。
我創建了一個腳本過程,用實際值 [例如,來自先前的節點創建事件] 替換 json 文件中的變量占位符。
如果我將強類型 object 添加到變量解析器中,這將非常有用...
因為變量解析器使用反射來查找變量值上的屬性名稱,並在 json 中進行替換。 但是如果添加到解析器的變量是JsonObject,我需要的屬性不可用。
例子:
我在解析器中設置了一個名為“project”的變量,來自此方法的 output。 [projectResponse.rootNode]
private ProjectResponse createProject(String project) {
ProjectCreateRequest createProject = new ProjectCreateRequest()
.setName(project)
.setSchemaRef("folder");
return this.adminClient.createProject(createProject).blockingGet();
}
Json 文件 -
第一個 json 文件有效,因為我將項目 NodeReference 添加到變量解析器 -
{
"parentNode" : {
"uuid" : "<project.uuid>"
},
"schema" : {
"name" : "folder"
},
"language" : "en",
"fields" : {
"name" : "node1 - child of project root node"
}
}
該創建的響應是一個 JsonObject,然后我將其傳遞給變量解析器。
然后我創建第二個節點。
注意我使用的是通用 post 方法[我不知道如何從 json 字符串創建 NodeCreateRequest,這也可以解決這個問題]
private JsonObject createNode(String project, String processedNode) {
JsonObject request = new JsonObject(processedNode);
JsonObject response = this.adminClient.post(String.format("/%s/nodes", project), request).blockingGet();
return response;
}
第二個 json 文件不起作用,因為node1
是 JsonObject,並且沒有 uuid 屬性 -
{
"parentNode" : {
"uuid" : "<node1.uuid>"
},
"schema" : {
"name" : "folder"
},
"language" : "en",
"fields" : {
"name" : "node2 - child of node1"
}
}
我不能將 JsonObject 自動 map 到 NodeResponse - FieldMap 有許多不同的實現,所以我不知道是否可以添加 Mapper 模塊來解決這個問題。
private NodeResponse createNode(String project, String processedNode) {
JsonObject request = new JsonObject(processedNode);
JsonObject response = this.adminClient.post(String.format("/%s/nodes", project), request).blockingGet();
return response.mapTo(NodeResponse.class);
}
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
com.gentics.mesh.core.rest.node.FieldMap (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.gentics.mesh.core.rest.node.NodeResponse["fields"])
(no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.gentics.mesh.core.rest.node.NodeResponse["fields"])
I think I've solved a similar use case by wrapping a Map in another class, then when the reflection occurs, I return my own property values based on the keys in the Map... but that was C#... and a long過去。
原來我使用的模板引擎可以處理 Map object,並像訪問屬性一樣訪問密鑰。
我通過將 JsonObject 轉換為 HashMap 來修復它。
return response.mapTo(HashMap.class);
如果您有 JSON 字符串,您可以使用 JsonUtil#readValue 將其轉換為任何 class。 如果您使用的是 Mesh Rest 客戶端,我認為這應該暴露出來。 因此,在您的情況下,對於請求,您可以使用JsonUtil.readValue(json, NodeCreateRequest.class)
。
如果你還想使用泛型方法,我認為最好的方法是將 JsonObject 轉換為 String ,然后使用 readValue 方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.