gpt4 book ai didi

java - 如何使用 Jackson 将 Java Map 转换为组合的 POJO?

转载 作者:行者123 更新时间:2023-12-02 04:46:10 24 4
gpt4 key购买 nike

我一直在尝试更可靠的方法来解析 Conductor 工作流程的输入。为此,我创建了一个包含人员及其车辆的组合类结构。

Person.java

public class Person {

protected String firstName;
protected String lastName;
protected List<Vehicle> vehicles;

// Constructors, getters, setters

}

Vehicle.java

public class Vehicle {

public String make;
public String model;
public String trim;
public short year;

// Constructors, getters, setters

}

然后,我创建了一个工作程序,它使用 Jackson 将 Conductor Client 的 Task.getInputData() 映射转换为上述 POJO。

ExperimentalStepWorker.java

public class ExperimentStepWorker implements Worker {

protected ObjectMapper objectMapper = new ObjectMapper();

// Other @Overloaded methods per the Worker interface

/**
* Executes a task and returns the updated task.
*
* @param task Task to be executed.
* @return the {@link TaskResult} object
* If the task is not completed yet, return with the status as IN_PROGRESS.
*/
@Override
public TaskResult execute(Task task) {

TaskResult result = new TaskResult(task);

Person person = objectMapper.convertValue(task.getInputData(), Person.class);

System.out.println("The map's contents were:");
System.out.println(task.getInputData());
System.out.println();
System.out.println("The objects's contents were:");
System.out.println(person);

result.setStatus(TaskResult.Status.COMPLETED);

return result;
}
}

然后,我向 Conductor 提交了以下工作流程。第一个没有输入。第二个只有一个 Person 对象,最后一个有一个包含三辆车的数组:

从 Postman 到 Conductor Server 的工作流程输入

{
"name": "see_if_jackson_works",
"workflowDef": {
"ownerApp": "postman",
"createdBy": "postman_user",
"name": "do_all_the_things_2",
"description": "Test Http Task Workflow",
"version": 1,
"tasks": [
{
"name": "worker_1",
"taskReferenceName": "First Experiment",
"inputParameters": {},
"type": "SIMPLE",
"startDelay": 15,
"optional": false
},
{
"name": "worker_2",
"taskReferenceName": "Second Experiment",
"inputParameters": {
"firstName": "Kirkland",
"lastName": "Yuknis"
},
"type": "SIMPLE",
"startDelay": 30,
"optional": false
},
{
"name": "worker_3",
"taskReferenceName": "Third experiment",
"inputParameters": {
"firstName": "Kirkland",
"lastName": "Yuknis",
"vehicles": [
{"make": "Subaru", "model": "Outback", "trim": "Limited", "year": 2018},
{"make": "Subaru", "model": "Outback", "trim": "Limited", "year": 2018},
{"make": "Subaru", "model": "Outback", "trim": "Limited", "year": 2018}
]
},
"type": "SIMPLE",
"startDelay": 45,
"optional": false
}
],
"schemaVersion": 2,
"restartable": true,
"workflowStatusListenerEnabled": false
},
"input": {}
}

前两个实验似乎进展顺利,但在最后一个实验中,指挥未能执行任务。阅读文档后,我想知道是否可以将组合对象作为 Conductor 任务的输入。有谁能证实或否认这一点吗?如果可以的话,我可能哪里做错了?

服务器调用 Worker 时 execute() 方法的 Worker 输出

The map's contents were:
{}

The objects's contents were:
Person{firstName='null', lastName='null', vehicles=null}

The map's contents were:
{firstName=Kirkland, lastName=Yuknis}

The objects's contents were:
Person{firstName='Kirkland', lastName='Yuknis', vehicles=null}

更新:我刚刚发现以下错误,这看起来像是 Jackson 问题

{
"taskType": "worker_3",
"status": "FAILED",
"inputData": {
"firstName": "Kirkland",
"lastName": "Yuknis",
"vehicles": [
{
"make": "Subaru",
"model": "Outback",
"trim": "Limited",
"Year": 2018
},
{
"make": "Subaru",
"model": "Outback",
"trim": "Limited",
"Year": 2018
},
{
"make": "Subaru",
"model": "Outback",
"trim": "Limited",
"Year": 2018
}
]
},
"referenceTaskName": "Third experiment",
"retryCount": 0,
"seq": 3,
"pollCount": 1,
"taskDefName": "worker_3",
"scheduledTime": 1559838042119,
"startTime": 1559838087887,
"endTime": 1559838088012,
"updateTime": 1559838087888,
"startDelayInSeconds": 45,
"retried": true,
"executed": false,
"callbackFromWorker": true,
"responseTimeoutSeconds": 1200,
"workflowInstanceId": "08a7bf21-6f4b-4265-97b0-0ce9cc93d1a4",
"workflowType": "do_all_the_things_2",
"taskId": "3b4906df-7136-4669-a8c8-a12a9b7c2f6a",
"reasonForIncompletion": "Error while executing the task: java.lang.IllegalArgumentException: Unrecognized field \"vehicles\" (class com.yuknis.egypt.models.Person), not marked as ignorable (2 known properties: \"lastName\", \"firstName\"])\n at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.yuknis.egypt.models.Person[\"vehicles\"])",
"callbackAfterSeconds": 0,
"workerId": "Kirklands-MBP.hsd1.fl.comcast.net",
"workflowTask": {
"name": "worker_3",
"taskReferenceName": "Third experiment",
"inputParameters": {
"firstName": "Kirkland",
"lastName": "Yuknis",
"vehicles": [
{
"make": "Subaru",
"model": "Outback",
"trim": "Limited",
"Year": 2018
},
{
"make": "Subaru",
"model": "Outback",
"trim": "Limited",
"Year": 2018
},
{
"make": "Subaru",
"model": "Outback",
"trim": "Limited",
"Year": 2018
}
]
},
"type": "SIMPLE",
"startDelay": 45,
"optional": false,
"taskDefinition": {
"createTime": 1559837812844,
"name": "worker_3",
"retryCount": 3,
"timeoutSeconds": 0,
"timeoutPolicy": "TIME_OUT_WF",
"retryLogic": "FIXED",
"retryDelaySeconds": 600,
"responseTimeoutSeconds": 1200,
"concurrentExecLimit": 100,
"rateLimitPerFrequency": 50,
"rateLimitFrequencyInSeconds": 60
},
"asyncComplete": false
},
"rateLimitPerFrequency": 0,
"rateLimitFrequencyInSeconds": 0,
"taskDefinition": {
"present": true
},
"queueWaitTime": 45768,
"taskStatus": "FAILED",
"logs": [
"06/06/19, 16:21:28:008 : java.lang.IllegalArgumentException: Unrecognized field \"vehicles\" (class com.yuknis.egypt.models.Person), not marked as ignorable (2 known properties: \"lastName\", \"firstName\"])\n at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.yuknis.egypt.models.Person[\"vehicles\"])\n\tat com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3750)\n\tat com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3668)\n\tat com.yuknis.egypt.workers.ExperimentStepWorker.execute(ExperimentStepWorker.java:58)\n\tat com.netflix.conductor.client.task.WorkflowTaskCoordinator.execute(WorkflowTaskCoordinator.java:379)\n\tat com.netflix.conductor.client.task.WorkflowTaskCoordinator.lambda$pollForTask$4(WorkflowTaskCoordinator.java:340)\n\tat java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)\n\tat java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\n\tat java.base/java.lang.Thread.run(Thread.java:835)\nCaused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field \"vehicles\" (class com.yuknis.egypt.models.Person), not marked as ignorable (2 known properties: \"lastName\", \"firstName\"])\n at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.yuknis.egypt.models.Person[\"vehicles\"])\n\tat com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)\n\tat com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)\n\tat com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)\n\tat com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3745)\n\t... 9 more\n"
]
},

最佳答案

我发现了问题。作为一种习惯,我不会为类上的非原始数据成员创建访问器。由于 List 是非基元,因此我(不假​​思索)没有为它创建访问器。除此之外,车辆受到保护,不公开。我添加了访问器并解决了问题。感谢所有看过此内容的人!

关于java - 如何使用 Jackson 将 Java Map 转换为组合的 POJO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56481784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com