Loading
 
What's Next: View All Tutorial →

A simple program to invoke webservice using java2wsdl plugin in Apache Camel CXF Component

In this example you how to invoke a Currecny convertor webservice using the camel cxf. Let write a program to invoke the currency conversion rate webservice http://www.webservicex.com/CurrencyConvertor.asmx Soap Request   <soapen...

Published Date: 04/05/2015  Last Modified Date: 04/05/2015    New Post



In this example you how to invoke a Currecny convertor webservice using the camel cxf.

Let write a program to invoke the currency conversion rate webservice
http://www.webservicex.com/CurrencyConvertor.asmx


Soap Request

 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:ConversionRate>
         <web:FromCurrency>USD</web:FromCurrency>
         <web:ToCurrency>INR</web:ToCurrency>
      </web:ConversionRate>
   </soapenv:Body>
</soapenv:Envelope>


Soap Response

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <ConversionRateResponse xmlns="http://www.webserviceX.NET/">
         <ConversionRateResult>63.615</ConversionRateResult>
      </ConversionRateResponse>
   </soap:Body>
</soap:Envelope>
 

Convert the wsdl  to Java package using Maven Plugin

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>Camel</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.15.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-cxf</artifactId>
            <version>2.15.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>                   
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>3.0.4</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>http://www.webservicex.com/CurrencyConvertor.asmx?wsdl</wsdl>
                                    <frontEnd>jaxws21</frontEnd>
                                    <faultSerialVersionUID>1</faultSerialVersionUID>
                                   
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

camel-cxf  - cxf camel dependency jar

cxf-codegen-plugin  - A plugin to conver the wsdl to java client jar

<wsdl>http://www.webservicex.com/CurrencyConvertor.asmx?wsdl</wsdl>   - wsdl url need to be passed to create a java file or the wsdl file can be store in the file and refer the file,


Jar creation process


Build the project build, After the sucessful build in the (Netbeans) IDE, the source will be generated in the Generated Source folder(Cxf).

The serviceClass will be created CurrencyConvertorSoap.java.

The serviceClass has the @Webservice method

For this Currency convertor, the service class method looks like,
@WebService(targetNamespace = "http://www.webserviceX.NET/", name = "CurrencyConvertorSoap")
@XmlSeeAlso({ObjectFactory.class})
public interface CurrencyConvertorSoap {

   @WebMethod(operationName = "ConversionRate", action = "http://www.webserviceX.NET/ConversionRate")
    @RequestWrapper(localName = "ConversionRate", targetNamespace = "http://www.webserviceX.NET/", className = "net.webservicex.ConversionRate")
    @ResponseWrapper(localName = "ConversionRateResponse", targetNamespace = "http://www.webserviceX.NET/", className = "net.webservicex.ConversionRateResponse")
    @WebResult(name = "ConversionRateResult", targetNamespace = "http://www.webserviceX.NET/")
    public double conversionRate(
        @WebParam(name = "FromCurrency", targetNamespace = "http://www.webserviceX.NET/")
        net.webservicex.Currency fromCurrency,
        @WebParam(name = "ToCurrency", targetNamespace = "http://www.webserviceX.NET/")
        net.webservicex.Currency toCurrency
    );
}

It say, two param "FromCurrency" and "ToCurrency" is input and the "double" value is the output


Let's create the Camel Cxf implementation.

 
package com.mycompany.camel;

import net.webservicex.Currency;
import org.apache.camel.CamelContext;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class CamelCxf {

    public static void main(String args[]) throws Exception {

        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                from("direct:start")
                        .to("cxf://http://www.webservicex.com/CurrencyConvertor.asmx?serviceClass=net.webservicex.CurrencyConvertorSoap")
                        .end();
            }
        });
        context.start();
        Thread.sleep(9000);

        ProducerTemplate producer = context.createProducerTemplate();

        Object a[] = new Object[2];
        a[0] = Currency.USD;
        a[1] = Currency.INR;

        Object res = producer.sendBody("direct:start", ExchangePattern.InOut, a);
        System.out.println(res);

    }

}

cfx://   - For cfx endpoint, we need to pass the webservice url and the serviceClass class name

producer.sendBody - to pass the two parameter in the exchange body and get the output

Please refere previous article for passing input to the function

Output:
[                          main] DefaultCamelContext            INFO  Apache Camel 2.15.1 (CamelContext: camel-1) is starting
[                          main] ManagedManagementStrategy      INFO  JMX is enabled
[                          main] DefaultTypeConverter           INFO  Loaded 197 type converters
[                          main] DefaultCamelContext            INFO  AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
[                          main] DefaultCamelContext            INFO  StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
[                          main] ReflectionServiceFactoryBean   INFO  Creating Service {http://www.webserviceX.NET/}CurrencyConvertorSoapService from class net.webservicex.CurrencyConvertorSoap
[                          main] DefaultCamelContext            INFO  Route: route1 started and consuming from: Endpoint[direct://start]
[                          main] DefaultCamelContext            INFO  Total 1 routes, of which 1 is started.
[                          main] DefaultCamelContext            INFO  Apache Camel 2.15.1 (CamelContext: camel-1) started in 3.575 seconds
[63.615]


Download the project


https://drive.google.com/file/d/0B_9jt9kIlxcibVBta2ttc2k3NVU/view?usp=sharing


https://github.com/Catchmycity/ApacheCamel/tree/master/ApacheCamel/CamelCFXWebserviceCurrencyCalculator
 
Awaiting for Administrator approval





Tags: Basic CXF component

← Back to list


Related Post




×