Home > Enterprise >  Correct use of <unmarshal in Spring DSL for JSON array
Correct use of <unmarshal in Spring DSL for JSON array

Time:07-10

Problem

I have a route ( Spring DSL ) which needs to take a JSON list from the api call in the route, render the JSON list as object(s), and pass the objects to a Processor for further action. So far, I have ( I think ) most of the pieces of the puzzle as indicated below.

Assuming this is an appropriate strategy for accomplishing my goal, what I am stuck on is the proper way to reference the Order class in the unmarshal line... thoughts ?

Route definition

<route id="rod-marshal-to-bean">

    <from uri="timer://fetchData?repeatCount=1"/>

    <to uri="api:fetch1?filter=Rod_Filter1&amp;batchSize=5&amp;connection={{connection-info}}"/>

    <split>
        <simple>${body}</simple>
        <log message="Body is : ${body}"/>
        <unmarshal ..../>   <--- stuck here
        <process id="process1" ref="customProcessor"/>
    </split>
</route>

Sample return value from Log message - JSON Array

[ {version=1, sku=HT-0001, label=NextGen-Rack ProSeries 1 Server}, {version=1, sku=HT-0002, label=NextGen-Rack ProSeries 2 Server, } ]

Class to handle the JSON Objects

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

class Order {
    @JsonProperty("version")
    private String version;
    
    @JsonProperty("sku")
    private String sku;
    
    @JsonProperty("label")
    private String label;

    String getVersion() {
        return version
    }

    void setVersion(String version) {
        this.version = version
        println "**** Setting version " version
    }

    String getSku() {
        return sku
    }

    void setSku(String sku) {
        this.sku = sku
        println "**** Setting sku " sku
    }

    String getLabel() {
        return label
    }

    void setLabel(String label) {
        this.label = label
        println "**** Setting label " label
    }
}

Custom Processor

import org.apache.camel.Exchange
import org.apache.camel.Processor

class CustomProcessor implements Processor {

    @Override
    void process(Exchange exchange) throws Exception {
        Order order = exchange.getIn().getBody(Order.class);
        .
        . Do some custom processing
        .

    }

}

CodePudding user response:

for xml dsl create a bean and pass this bean name to unmarshall method

@Bean("orderJacksonDataFormat")
    public JacksonDataFormat depositResponseFormatter() {
        return  new ListJacksonDataFormat(Order.class);
}

for java dsl unmarshall it

// fetch data
.unmarshal(new ListJacksonDataFormat(Order.class))
.process(customProcessor)

use in processor

class CustomProcessor implements Processor {

    @Override
    void process(Exchange exchange) throws Exception {
        List<Order> order = (List<Order>)exchange.getIn().getBody(List.class);
        .
        . Do some custom processing
        .

    }

}
  • Related