Error: SPIN/JACKSON-JSON-01006 Cannot deserialize

Hello,

I got error below:

`Cannot submit task form b16335d9-af77-11eb-a577-561dc61868be: Cannot deserialize object in variable 'pdcEvaluationGroup': SPIN/JACKSON-JSON-01006 Cannot deserialize '[{"id":"ok...' to java type '[collection type; class java.util.ArrayList, contains [simple type, class com.mobven.camunda.identity.model.AuthUser]]'`
  • Actually, I created the pdcEvaluationGroup variable second activity in the model which stored a list of users coming from the camunda identity service.
  • I assign the same task to each user and get a result from them in this list. (think like voting);
  • After that gateways and another task works. But almost 3 activities later, I got this error. When I checked the cockpit you can see screenshots below, type is Object, not deserialize.
    *When I change the type to JSON, the error went out but I want to use this variable again in the next steps. At that point, I got another error which says it is not a collection.

So I checked docs but still confused about the issue.

1 Like

@Oktay_Ibis You can change the serialized Json string to collection like below:

SpinJsonNode spinJsonNode = Spin.JSON(execution.getVariable("pdcEvaluationGroupJson"));

//If the list values is type of primitives:
List<String> pdcEvaluationGroup = spinJsonNode.mapTo(List.class);
execution.setVariable("pdcEvaluationGroup",pdcEvaluationGroup);

//If type is custom object, and the java class doesn't exist in the classpath:
List<String> usersList = new ArrayList<>();
usersList.add(spinJsonNode.jsonPath("username").stringValue());  //extract username from the array
execution.setVariable("pdcEvaluationGroup",usersList);

//If type is custom object, and the java class exist in the classpath:
List<AuthUser> pdcEvaluationGroupList = spinJsonNode.mapTo(List.class);
List<String> usersList = new ArrayList<>();
pdcEvaluationGroupList.forEach(group->{usersList.add(group.getUsername())});
execution.setVariable("pdcEvaluationGroup",usersList);

Now use this variable β€œpdcEvaluationGroup” as the collection variable for assignments.


Refer the below example:

BPMN File: voting.bpmn (3.7 KB)

External Worker configuration which sets the variable votersListJsonValue as serialized json value with type as Json:

@Slf4j
@Component
public class VotersPreparationWorker {

	@Autowired
	private Gson gsonParser;

	@Autowired
	private ExternalWorkerConfiguration externalWorkerConfiguration;

	@EventListener(ApplicationReadyEvent.class)
	public void handleExternalTasks() {
		log.info("Initialzing the ExternalTaskServiceWorker with configuration:{}", externalWorkerConfiguration);

		ExternalTaskClient externalTaskClient = ExternalTaskClient.create()
				.baseUrl(externalWorkerConfiguration.getBaseUrl())
				.addInterceptor(requestContext -> requestContext.addHeader("Authorization",
						externalWorkerConfiguration.getAuthorization()))
				.asyncResponseTimeout(1000).maxTasks(2).workerId("FETCH_APPROVERS").build();

		TopicSubscription topicSubscription = externalTaskClient.subscribe("FETCH_APPROVERS")
				.handler((externalTask, externalTaskService) -> {
					List<Voters> votersList = new ArrayList<>();
					
					votersList.add(Voters.builder().username("myself1").uniqueId("XC02334241").age(25).city("mycity1")
							.email("myemail1").allowedToVote(true).build());
					votersList.add(Voters.builder().username("myself2").uniqueId("XC02334242").age(25).city("mycity2")
							.email("myemail2").allowedToVote(true).build());
					
					JsonValue votersListJsonValue = ClientValues.jsonValue(gsonParser.toJson(votersList));

					Map<String, Object> variables = new HashMap<>();
					variables.put("votersListJsonValue", votersListJsonValue);

					externalTaskService.complete(externalTask, variables);

					log.info("ExternalTask {} with businessKey {} completed!!!", externalTask.getId(),
							externalTask.getBusinessKey());

				}).lockDuration(30000).open();

		log.info("ExternalTaskServiceWorker has subscribed to the topic {} sucessfully",
				topicSubscription.getTopicName());
	}

}

Voter object which i kept part of the external task worker.

@Getter
@Builder
public class Voters implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String username;
	
	private String email;
	
	private String city;
	
	private int age;
	
	private String uniqueId;
	
	private boolean allowedToVote;

}

Now, the Voters object doesn’t present in the process application and I have the serialized json value for the list of Voters object.

I have configured the below Execution listener(PrepareVotersListListener) to the event type β€œtake” for the outgoing sequence from external task.

Now extracting the users list will be like:

@Slf4j
@Component
public class PrepareVotersListListener implements ExecutionListener {

	@Override
	public void notify(DelegateExecution execution) {
		log.info("Executing PrepareVotersListListener.....");
		SpinJsonNode spinJsonNode = Spin.JSON(execution.getVariable("votersListJsonValue"));
		List<String> votersList = new ArrayList<>();
		spinJsonNode.elements().forEach(elem ->{
			String username = elem.jsonPath("username").stringValue();
			log.info("Username of the voter:{}", username);
			votersList.add(username);
		});
		execution.setVariable("votersList", votersList);
	}

}

For using SpinJsonNode make sure below dependencies are in the classpath of process apllication:

 <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.camunda.spin</groupId>
        <artifactId>camunda-spin-bom</artifactId>
        <scope>import</scope>
        <type>pom</type>
        <version>1.9.0</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

   <dependency>
      <groupId>org.camunda.spin</groupId>
      <artifactId>camunda-spin-core</artifactId>
    </dependency>

    <dependency>
      <groupId>org.camunda.spin</groupId>
      <artifactId>camunda-spin-dataformat-json-jackson</artifactId>
    </dependency>

    <dependency>
      <groupId>org.camunda.spin</groupId>
      <artifactId>camunda-spin-dataformat-xml-dom</artifactId>
    </dependency>

JSON Value inserted from worker for the Voters object like:

From process application, using spinnode the extracted users list value looks like:

Cockpit view of the process history:

1 Like