Monday, August 29, 2011

MULE HTTP Web Service - Step2A. Creating the client jars from the WSDL with Options

This step focuses on creating the client jars from the WSDL created in the previous step. This serves two purposes.
  1. To verify that the WSDL and the XSD was created without any errors.
  2. To get the jars ready so that we will have the class files generated for the objects defined in XSD. These classes will be used in our Mule web service.
This example focuses on using the Maven cxf-codegen-plugin to generate the classes.

Since we are going to develop a Mule Web Service using maven, we can easily import the client jar in your pom.xml

1. Create a simple maven project in Eclipse.



2. Add the cxf-codegen-plugin to your pom.xml

<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>org.hello</groupId>
    <artifactId>HelloServiceClient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>2.4.0</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <extraargs>
                                        <extraarg>-p</extraarg>
                                        <extraarg>http://hello.services.com=com.services.hello.helloservice</extraarg>
                                        <extraarg>-p</extraarg>
                                        <extraarg>http://ws.hello.services.com=com.services.hello.helloservice.ws</extraarg>
                                    </extraargs>
                                    <wsdl>${basedir}/src/main/resources/Hello.wsdl</wsdl>
                                    <bindingFiles>
                                        <bindingFile>${basedir}/src/main/resources/bindings.xml</bindingFile>
                                    </bindingFiles>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3. Move the WSDL and XSD created in the previous section under src/main/resources

4. Make sure that you have JDK in your project classpath and not JRE. This plugin needs to compile and generate the class files and it requires a JDK to do it.

5. Right click on the project, go to Run As and select Maven Package.

With the above steps the client jar would get generated successfully and the client jar should contain the following structure.


No comments:

Post a Comment