Invoking a remote web service from Camel

Camel's CXF Component makes calling a web service easy by defaulting to using POJOs for the request and response objects, and storing all of the other SOAP details in exchange headers in case you need them.

With Camel's built-in data type conversion and bean parameter-binding capabilities, you can easily call out to your Java methods that expect the body of the SOAP message as either a parameter or as a return type. Camel will automatically handle the conversions between Java and XML. When calling a web service, additional details, such as which operation name to invoke, are set in the exchange separately from the body of the message.

This recipe will show you the basic structure for calling a web service from a Camel route.

Getting ready

This recipe assumes that you have a project with JAX-WS artifacts created as shown in the Generating the service stubs from a WSDL recipe. To use the generated API, you need to include a dependency to that project in your build:

<dependency>
  <groupId>org.camelcookbook.examples</groupId>
  <artifactId>ws-payments-api</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

All the source files for this recipe are located in the ws-camel-routes project in the camel-cookbook-web-services module. The Java code for this recipe is located in the org.camelcookbook.ws.client package. The Spring XML files are located under src/main/resources/META-INF/spring and are prefixed with client.

How to do it...

To invoke a web service from Camel using the Camel CXF Component, perform the following steps:

  1. Configure the web service endpoint that will be invoked by defining a cxfEndpoint element outside the camelContext element. This element is found in the Camel CXF namespace (http://camel.apache.org/schema/cxf). It defines the address of the remote service (address), and the interface class that was generated from the WSDL (serviceClass).
    <beans ...
        xmlns:cxf="http://camel.apache.org/schema/cxf"
        xsi:schemaLocation="...
          http://camel.apache.org/schema/cxf
          http://camel.apache.org/schema/cxf/camel-cxf.xsd">
    
      <cxf:cxfEndpoint
          id="paymentServiceEndpoint"
          address="http://localhost:1234/paymentService"
          serviceClass=
            "org.camelcookbook.ws.payment_service.Payment"/>
      <!-- ... -->
    </beans>

    The serviceClass is the interface generated from the WSDL. The address represents the URL of the remote service that CXF will invoke.

    In the Java DSL, since we cannot use Spring namespace handlers (that is, cxfEndpoint), we simply create an endpoint URI string:

    final String cxfUri =
      "cxf:http://localhost:1234/paymentService?" +
      "serviceClass=" + Payment.class.getName();
  2. Invoke the web service.

    In the XML DSL, reference the endpoint that you configured in step one using the cxf:bean: prefix with the operation name that you want to trigger, as well as any additional options specific to this route step:

    <camelContext 
        xmlns="http://camel.apache.org/schema/spring">
      <route id="wsClient">
        <from uri="direct:start"/>
        <log message="${body}"/>
        <to uri="cxf:bean:paymentServiceEndpoint?defaultOperationName=transferFunds"
        />
      </route>
    </camelContext>

    In the Java DSL, we append any additional options to our endpoint URI:

    from("direct:start")
        .id("wsClient")
      .log("${body}")
      .to(cxfUri + "&defaultOperationName=transferFunds");
    

How it works...

This code example assumes that the body of Camel exchange is of the type TransferRequest, which is the expected request parameter type specified in the WSDL for the transferFunds operations. The Camel CXF Component will automatically wrap the message body with the appropriate SOAP envelope including any other SOAP header information configured by the endpoint for security.

In the case of request-response web service operations, the Camel CXF Component will extract the SOAP body of the response, convert it into a POJO, and place it into the body of exchange for further processing by the route.

A lot happens under the covers within the interaction between the Camel and CXF frameworks to correctly wrap and unwrap the POJO message into a web service invocation. This recipe demonstrates only the most basic structure for invoking a web service from Camel. Please see the Camel and CXF documentation in the See also section of this recipe for the full range of options available to you when dealing with SOAP web services.

There's more...

In this example, we configured the endpoint with a defaultOperationName that will have the Camel CXF Component invoke that operation. You can also set the operationName header on the exchange to the web service operation to call. That header's value will override any default operation name configured on the endpoint.

The Camel CXF Component has a dataFormat option that allows you to specify that you want the data in a format other than the default POJO mode. For example, to process the raw SOAP XML message, set dataFormat=MESSAGE. See the Camel CXF Component documentation for full details.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.71.211