【问题标题】:Spring Integration XML - Unmarshall before send to channelSpring Integration XML - 在发送到通道之前解组
【发布时间】:2016-02-10 22:16:42
【问题描述】:

在我的应用程序中,我有一个将 XML 标记动态发送到适当通道的路由器:

<!-- Route each tag to the appropiate channel -->
<int-xml:xpath-router id="router" input-channel="routerChannel" evaluate-as-string="true">
    <int-xml:xpath-expression expression="concat(name(./node()), 'Channel')" />
</int-xml:xpath-router>

使用此 XML 将每个标签路由到 serviceChannelactivityChannel

<root>
  <service attr1="x" attr2="y" />
  <activity anotherAttr="W" />
</root>

我的服务激活器 POJO 是这样的:

class Service {
  private String attr1;
  private String attr2;

  /* Setter and getter omitted */
}

这是 applicationContext.xml 配置:

<!-- Service activators --> 
  <int:service-activator input-channel="serviceChannel" method="schedule">
    <bean class="it.mypkg.Service" />
  </int:service-activator>

路由器将整个Node 发送到bean,我需要“手动”提取属性。

有一种方法可以“解组”Node,所以在 attr1attr2 中,我将获得 XML 中提供的值?

通常我使用一个简单的 Unmarshaller(使用 JAXB)并添加注释 @XmlRootElement@XmlAttribute 等等。

我认为在传递给 POJO 之前,我需要使用 SI-XML UnmarshallingTransformer,但我真的不知道该怎么做......而且这应该是“足够通用”来处理所有标签(当然,所有 POJO 类,如 @ 987654334@ 和 Activity 将有 @XmlAttribute 等)

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    使用Unmarshalling Transformer,然后在未编组的对象上路由。

    【讨论】:

    • 这是我的想法,但我不明白 unmarshallingTransformer 中的某些内容,例如链接中的属性 contextPath 的值为 org.example。你可以给我一个如何实现它的小例子吗?
    • 上下文路径只是 JAXB 注释类所在的包。请参阅 JAXBContext.newInstance()spring reference manual 的 javadocs。
    猜你喜欢
    • 2017-12-13
    • 2018-05-30
    • 2018-04-15
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多