4
votes

Since i do no have enough reputation to comment on the other topic open about this

(How to unmarshall xml using spring integration dsl)

I had to create a new topic in order to ask my question. I have a JMS which polls XML messages from a queue, and i want to transform those messages into Java Objects. Here is the code i have written

@Bean
public IntegrationFlow jmsMessageDrivenRedeliveryFlow() {
    return IntegrationFlows
            .from(Jms.messageDrivenChannelAdapter(new ActiveMQConnectionFactory("tcp://localhost:61616"))
                    .errorChannel(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)
                    .destination("foo.bar").jmsMessageConverter(new MarshallingMessageConverter(jaxbMarshaller())))
            .handle(m -> System.out.println(m.getName())).get();
}

@Bean
public Marshaller jaxbMarshaller() {
    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
    return jaxb2Marshaller;
}

The class that i want to Create from XML is Customer class with the following structure

@XmlRootElement
public class Customer {

    String name;
    int age;
    int id;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }

}

Based on the answers on the other thread, how can i configure the Marshaller to return a Customer object? Thanks a lot in advance.

1

1 Answers

3
votes

I see you have this .jmsMessageConverter(new MarshallingMessageConverter(jaxbMarshaller()))) in your Jms.messageDrivenChannelAdapter(). That's everything you need from the Spring Integration perspective.

Since you still have a question I guess your problem is how to teach that Jaxb2Marshaller to see your Customer and unmarshal properly.

This is already a JAXB question: https://www.oracle.com/technetwork/articles/javase/index-140168.html.

And is a property like this in the mentioned Jaxb2Marshaller:

/**
 * Set the list of Java classes to be recognized by a newly created JAXBContext.
 * <p>Setting either this property, {@link #setContextPath "contextPath"}
 * or {@link #setPackagesToScan "packagesToScan"} is required.
 */
public void setClassesToBeBound(@Nullable Class<?>... classesToBeBound) {