繁体   English   中英

如何发送 JsonObject 请求,并在 MeshRestClient 中接收 NodeResponse?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM