简体   繁体   English

在 spring 集成中将 XML 转换为 Java 对象的正确方法是什么?

[英]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.我一直在寻找一种将 XML 到 Java 对象转换器合并到我的 spring 集成服务中的方法。 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.这是我确定必须已经存在的东西,但我遇到的问题是我一直在寻找基于 Spring 集成的旧版本的文档和教程。

The class that I'm trying to convert to has been annotated with javax.xml.bind.annotation.我尝试转换为的类已使用 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]

Not sure what you are going to reach, but your StackTrace says everything you need and you have missed:不确定你会达到什么,但你的 StackTrace 说明了你需要的一切,但你错过了:

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 .其中unmarshallingTransformerUnmarshallingTransformer类型,但这要求它像org.springframework.oxm.Unmarshaller

Consider to configure Jaxb2Marshaller bean and use it as a reference to that unmarshaller property.考虑配置Jaxb2Marshaller bean 并将其用作对该unmarshaller属性的引用。

You don't need to configure UnmarshallingTransformer manually.您不需要手动配置UnmarshallingTransformer The <int-xml:unmarshalling-transformer> will do that for you. <int-xml:unmarshalling-transformer>将为您做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM