4

私はSpring Integrationプロジェクトをやっています。アンマーシャリング トランスフォーマーを介して、XML 文字列を 1 つのチャネルに公開し、別のチャネルで Java オブジェクトを受信したいと考えています。

私のSpring構成では、これらのチャネルとBeanを使用しています:

<!-- channel to for publishing XML string -->
<int:publish-subscribe-channel id="channels.pubsub.inXml" />

<!-- channel for receiving transformed reply -->
<int:channel id="channels.fromXmlChannel">
    <int:queue capacity="10" />
</int:channel>

<!-- transformer unmarshalling XML to Java object -->
<int-xml:unmarshalling-transformer
    unmarshaller="marshaller"
    input-channel="channels.pubsub.inXml"
    output-channel="channels.fromXmlChannel" />

<!-- marshaller bean -->
<bean id="marshaller" class="org.springframework.oxm.castor.CastorMarshaller" />

単体テストでこれを試しているので、使用する自動配線チャネルがあります。

@Autowired @Qualifier("channels.pubsub.inXml")
MessageChannel inXmlChannel;

@Autowired @Qualifier("channels.fromXmlChannel")
MessageChannel fromXmlChannel;

アンマーシャリングしたい Trade クラスは非常に些細な POJO です。

class Trade {
    String id;
    String direction;
    // getters, setters, constructors
}

これが私のJUnitテストメソッドですが、3行目で例外が発生します:

    @Test
public void testUnmarshallingTransformer() {
    String xml = "<trade id='ID-01' direction='DIR-PRG' />";

    Message<String> message = MessageBuilder.withPayload(xml).build();
    inXmlChannel.send(message, TIMEOUT);
    Message<?> received = ((PollableChannel) fromXmlChannel).receive(TIMEOUT);

    System.out.println("\n\n" + received.getPayload());
}

そして最後に、スタックトレースは次のとおりです。

org.springframework.integration.transformer.MessageTransformationException: failed to transform message
    at org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:44)
    at org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:67)
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:97)
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
    at org.springframework.integration.dispatcher.BroadcastingDispatcher.invokeHandler(BroadcastingDispatcher.java:105)
    at org.springframework.integration.dispatcher.BroadcastingDispatcher.dispatch(BroadcastingDispatcher.java:96)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:61)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
    at sk.xorty.transformers.BuiltinTransformersTest.testUnmarshallingTransformer(BuiltinTransformersTest.java:190)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.springframework.oxm.UnmarshallingFailureException: Castor unmarshalling exception; nested exception is org.exolab.castor.xml.MarshalException: The class for the root element 'trade' could not be found.{File: [not available]; line: 1; column: 41}
    at org.springframework.oxm.castor.CastorMarshaller.convertCastorException(CastorMarshaller.java:487)
    at org.springframework.oxm.castor.CastorMarshaller.unmarshalReader(CastorMarshaller.java:399)
    at org.springframework.oxm.support.AbstractMarshaller.unmarshalStreamSource(AbstractMarshaller.java:371)
    at org.springframework.oxm.support.AbstractMarshaller.unmarshal(AbstractMarshaller.java:134)
    at org.springframework.integration.xml.transformer.UnmarshallingTransformer.transformPayload(UnmarshallingTransformer.java:107)
    at org.springframework.integration.transformer.AbstractPayloadTransformer.doTransform(AbstractPayloadTransformer.java:33)
    at org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:33)
    ... 39 more
Caused by: org.exolab.castor.xml.MarshalException: The class for the root element 'trade' could not be found.{File: [not available]; line: 1; column: 41}
    at org.exolab.castor.xml.Unmarshaller.convertSAXExceptionToMarshalException(Unmarshaller.java:866)
    at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:763)
    at org.springframework.oxm.castor.CastorMarshaller.unmarshalReader(CastorMarshaller.java:396)
    ... 44 more
Caused by: org.xml.sax.SAXException: The class for the root element 'trade' could not be found.
    at org.exolab.castor.xml.UnmarshalHandler.processFirstElement(UnmarshalHandler.java:890)
    at org.exolab.castor.xml.StartElementProcessor.compute(StartElementProcessor.java:103)
    at org.exolab.castor.xml.UnmarshalHandler.startElementProcessing(UnmarshalHandler.java:811)
    at org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:733)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:766)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1302)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(XMLDocumentScannerImpl.java:1275)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3063)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:881)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:751)
    ... 45 more
4

6 に答える 6

1

CastorMarshaller の Java ドキュメントを参照してください。<property name="targerClass" value="foo.Trade"/> をマーシャラーに追加します。

于 2012-06-22T12:28:08.200 に答える
0

キャスター構成ファイルで、またはアンマーシャラー構成の一部として、タグをクラスにtradeマップするように指定する必要があります。これがここでの問題のようです。Trade

CastorMarshaller自体を使用して直接実行できるはずです。

<bean id="marshaller" class="org.springframework.oxm.castor.CastorMarshaller">
<property name="targetClasses>
 <list>
  <value>Trade.class</value>
 </list>
</property>
</bean>
于 2012-06-22T12:15:17.823 に答える
0

このプロパティを使用します。

marshaller.setUseXSITypeAtRoot(true);

次に、xml で、正確なクラスを持つルート要素に属性 xsi:type= が表示されます。アンマーシャラーはその後動作します。

于 2015-06-05T10:58:30.867 に答える
0

以下のコードを使用すると、この問題が解決します。

<bean id="marshaller" class="org.springframework.oxm.castor.CastorMarshaller">
 <property name="targetClasses" value="Trade">

</property>

Trade は pojo 名にする必要があります。

于 2013-06-27T17:47:38.957 に答える