简体   繁体   中英

Invoke SOAP based service using Apache Camel and spring-ws component

I have SOAP based service which accepts some predefined request object,

for example, AccountRequest having some fields.

Sample code

from("direct:test")
    .routeId("account.get")
    .process(exchange -> {
        exchange.getIn().setBody(createAccountRequest());
    })
    .to("spring-ws:http://localhost:8090/AccountServices/AccountOverviewService")
    .log("Got Request for account-detail");
}

Above code throws an error

org.apache.camel.NoTypeConversionAvailableException: 
No type converter available to convert from type: 
com.test.AccountRequest to the required type: 
javax.xml.transform.Source with value com.test.AccountRequest@4e1c1763

Is this right way to invoke soap service via camel?

Dependencies

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring-boot-starter</artifactId>
    <version>2.18.3</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring-ws</artifactId>
    <version>2.18.3</version>
</dependency>

Here's how my SOAP WS example looks using cxf .

Firstly, in camel-context.xml , define the web service bean:

<cxf:cxfEndpoint id="insuranceService"
                 address="http://localhost:8080/insuranceService"
                 serviceClass="com.mycompany.insurance.insurancePort"
                 wsdlURL="schema/InsuranceService.wsdl">
</cxf:cxfEndpoint>

Now the camel route looks like this:

from("somewhere")
    .to("cxf:bean:insuranceService")

And you might need some dependencies like these:

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-cxf</artifactId>
        <version>${framework.camel}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>${framework.cxf}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-http</artifactId>
        <version>${framework.camel}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jaxb</artifactId>
        <version>${framework.camel}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-soap</artifactId>
        <version>${framework.camel}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-saxon</artifactId>
        <version>${framework.camel}</version>
    </dependency>

You would need to marshal the "com.test.AccountRequest" to the xml by adding

  JaxbDataFormat jaxb = new JaxbDataFormat(false); //add
  jaxb.setContextPath("com.accountservice.model"); //add - path to your generated stubs

    from("direct:test")
        .routeId("account.get")
        .process(exchange -> {
            exchange.getIn().setBody(createAccountRequest());
        })
        .marshal(jaxb) //add
        .to("spring-ws:http://localhost:8090/AccountServices/AccountOverviewService")
        .log("Got Request for account-detail");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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