309.3. 使用 Java DSL

以下示例使用了名称 soap 的 DataFormat,它配置有软件包 com.example.customerservice 来初始化 JAXBContext。第二个参数是 ElementNameStrategy。路由也可以编入普通对象以及例外。(注意以下只是将 SOAP Envelope 发送至队列。Web 服务提供商实际上需要侦听实际发生的 SOAP 调用队列,在这种情况下,它是 SOAP 请求的一种方式。如果您需要请求答复,那么您应该查看下一个示例。)

SoapJaxbDataFormat soap = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
from("direct:start")
  .marshal(soap)
  .to("jms:myQueue");
提示

另请参阅,因为 SOAP 数据格式从 JAXB 数据格式大多数设置中继承。

309.3.1. 使用 SOAP 1.2

可从 Camel 2.11 开始

SoapJaxbDataFormat soap = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
soap.setVersion("1.2");
from("direct:start")
  .marshal(soap)
  .to("jms:myQueue");

当使用 XML DSL 时,您可以在 <SOApjaxb> 元素中设置 version 属性。

    <!-- Defining a ServiceInterfaceStrategy for retrieving the element name when marshalling -->
    <bean id="myNameStrategy" class="org.apache.camel.dataformat.soap.name.ServiceInterfaceStrategy">
        <constructor-arg value="com.example.customerservice.CustomerService"/>
    <constructor-arg value="true"/>
    </bean>

在 Camel 路由中

<route>
  <from uri="direct:start"/>
  <marshal>
    <soapjaxb contentPath="com.example.customerservice" version="1.2" elementNameStrategyRef="myNameStrategy"/>
  </marshal>
  <to uri="jms:myQueue"/>
</route>