Hula
June 14, 2021, 11:44am
1
I try to create a JSON for my List of HistoricProcessInstances.
How is the proper way of doing this?
My code:
List<HistoricProcessInstance> hpi = camunda.getHistoryService()
.createHistoricProcessInstanceQuery()
.variableValueEquals(name, value)
.list();
Stream<HistoricProcessInstance> stream = hpi.stream();
List<HistoricProcessInstance> streamData = stream.collect(Collectors.toList());
// create json
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(streamData.toString(), HistoricProcessInstance.class);
String jsonCase = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
You already have List<HistoricProcessInstance>
queried using .createHistoricProcessInstanceQuery()
in the first line. Then why are you’re iterating the same result data using streams and again collecting same result set?
hpi.stream();
Don’t Repeat Yourself (DRY)
This block of code will raise below error:
com.fasterxml.jackson.databind.JsonMappingException:
No suitable constructor found for type
To convert to json string:
List<HistoricProcessInstance> historicProcessInstanceList = historicProcessInstanceQuery.list();
List<HistoricProcessInstanceDto> historicProcessInstanceDtoList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(historicProcessInstanceList)) {
historicProcessInstanceList.forEach(historicProcessInstance -> {
HistoricProcessInstanceDto historicProcessInstanceDto = HistoricProcessInstanceDto
.fromHistoricProcessInstance(historicProcessInstance);
historicProcessInstanceDtoList.add(historicProcessInstanceDto);
});
ObjectMapper mapper = new ObjectMapper();
String jsonCase = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(historicProcessInstanceDtoList);
}
Hula
June 14, 2021, 1:23pm
3
Thanks,
I tried to do this without a loop but instead seems like I went in one…