【问题标题】:What is the proper way to convert XML to Java objects in spring integration?在 Spring 集成中将 XML 转换为 Java 对象的正确方法是什么?
【发布时间】:2017-01-27 11:12:54
【问题描述】:

我一直在寻找一种将 XML 到 java 对象转换器合并到我的 spring 集成服务中的方法。这是我确定一定已经存在的东西,但我遇到的问题是我一直在寻找基于旧版本的 spring 集成的文档和教程。

我尝试转换的类已经用 javax.xml.bind.annotation 进行了注释。

这是我迄今为止所提供的服务,基于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>

但是当我尝试这个时,我得到...

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]

【问题讨论】:

    标签: java xml spring spring-integration


    【解决方案1】:

    不确定您要达到什么目标,但您的 StackTrace 说明了您需要的所有内容,但您错过了:

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

    看,你的代码是这样的:

    unmarshaller="unmarshallingTransformer"
    

    其中unmarshallingTransformer 属于UnmarshallingTransformer 类型,但这要求它类似于org.springframework.oxm.Unmarshaller

    考虑配置Jaxb2Marshaller bean 并将其用作对该unmarshaller 属性的引用。

    您无需手动配置UnmarshallingTransformer&lt;int-xml:unmarshalling-transformer&gt; 将为您做到这一点。

    【讨论】:

    • 谢谢,配置 Jaxb2Marshaller 成功了。我对将 unmarshallingTransformer 声明为 bean 的示例感到困惑。
    猜你喜欢
    • 2021-07-30
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2015-05-22
    • 2012-08-16
    • 1970-01-01
    相关资源
    最近更新 更多