Apache CXF with WSDL First Example

www.igif‮‬tidea.com

example of how to use Apache CXF to create a web service starting from a WSDL file.

  1. First, we need to create a WSDL file that describes our web service. Let's create a file called "hello.wsdl" that will define a simple web service with a single method called "sayHello" that takes a String parameter and returns a String:
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="HelloService"
             targetNamespace="http://example.com/hello"
             xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:tns="http://example.com/hello"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <message name="sayHelloRequest">
        <part name="name" type="xsd:string"/>
    </message>

    <message name="sayHelloResponse">
        <part name="greeting" type="xsd:string"/>
    </message>

    <portType name="HelloPortType">
        <operation name="sayHello">
            <input message="tns:sayHelloRequest"/>
            <output message="tns:sayHelloResponse"/>
        </operation>
    </portType>

    <binding name="HelloBinding" type="tns:HelloPortType">
        <soap:binding style="document"
                      transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="sayHello">
            <soap:operation soapAction=""/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>

    <service name="HelloService">
        <port name="HelloPort" binding="tns:HelloBinding">
            <soap:address location="http://localhost:8080/hello"/>
        </port>
    </service>

</definitions>

In this file, we're defining a message for the "sayHello" request and response, a port type for our web service with the "sayHello" operation, a binding that defines how the SOAP messages will be transmitted over HTTP, and a service that exposes our web service at the "/hello" URL.

  1. Next, we need to use the CXF "wsdl2java" tool to generate Java code from our WSDL file. To do this, run the following command:
wsdl2java -d /path/to/output/dir hello.wsdl

This will generate Java classes in the specified output directory that correspond to the types and operations defined in the WSDL file.

  1. Now we can create an implementation of our web service by extending the generated Java classes. Let's create a class called "HelloServiceImpl" that will have a single method called "sayHello" that takes a String parameter and returns a String:
package com.example;

public class HelloServiceImpl implements HelloPortType {

    public String sayHello(String name) {
        return "Hello " + name;
    }

}

Note that we're implementing the "HelloPortType" interface that was generated from our WSDL file.

  1. Finally, we need to create a main method that will start up our web service. Here's what the main method should look like:
import javax.xml.ws.Endpoint;

public class HelloServicePublisher {

    public static void main(String[] args) {
        // Create the web service endpoint
        Endpoint.publish("http://localhost:8080/hello", new HelloServiceImpl());

        // Wait for the user to terminate the program