Ryan Stull Ryan Stull - 2 months ago 23
Java Question

What is the proper way to convert XML to Java objects in spring integration?

I've been searching for a way to incorporate an XML to java object converter into my spring integration service. This is something that I'm sure must already exist, but the issue I'm having is I keep finding docs and tutorials based on older version on spring integration.

The class that I'm trying to convert to has been annotated with javax.xml.bind.annotation.

This is what I have so far in my service, which was based off of this page...

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:file="http://www.springframework.org/schema/integration/file"
xmlns:feed="http://www.springframework.org/schema/integration/feed"
xmlns:jms="http://www.springframework.org/schema/integration/jms"
xmlns:int-xml="http://www.springframework.org/schema/integration/xml"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/integration/feed http://www.springframework.org/schema/integration/feed/spring-integration-feed.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
http://www.springframework.org/schema/integration/xml http://www.springframework.org/schema/integration/xml/spring-integration-xml-4.2.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">

.............

<bean id="unmarshallingTransformer" class="org.springframework.integration.xml.transformer.UnmarshallingTransformer">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.bottomline.cfrm.amq_wfs_bridge.model.audit" />
</bean>
</constructor-arg>
</bean>

<int-xml:unmarshalling-transformer id="defaultUnmarshaller"
input-channel="channel_1" output-channel="channel_2"
unmarshaller="unmarshallingTransformer"/>

.............

</beans>


But when I try this I get...

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.integration.xml.transformer.UnmarshallingTransformer#0': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.oxm.Unmarshaller]: Could not convert constructor argument value of type [org.springframework.integration.xml.transformer.UnmarshallingTransformer] to required type [org.springframework.oxm.Unmarshaller]: Failed to convert value of type [org.springframework.integration.xml.transformer.UnmarshallingTransformer] to required type [org.springframework.oxm.Unmarshaller]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.integration.xml.transformer.UnmarshallingTransformer] to required type [org.springframework.oxm.Unmarshaller]: no matching editors or conversion strategy found
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:724) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:760) ~[spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:360) ~[spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:306) ~[spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
at com.bridge.Application.main(Application.java:14) [classes/:na]

Answer

Not sure what you are going to reach, but your StackTrace says everything you need and you have missed:

Could not convert constructor argument value of type [org.springframework.integration.xml.transformer.UnmarshallingTransformer] to required type [org.springframework.oxm.Unmarshaller]

Look, your code is like:

unmarshaller="unmarshallingTransformer"

Where unmarshallingTransformer is of UnmarshallingTransformer type, but that requires it to be like org.springframework.oxm.Unmarshaller.

Consider to configure Jaxb2Marshaller bean and use it as a reference to that unmarshaller property.

You don't need to configure UnmarshallingTransformer manually. The <int-xml:unmarshalling-transformer> will do that for you.

Comments