Skip to main content

Code-first development

Code-first development means starting from an existing Java interface of a web service provider from which a WSDL can automatically be generated. Talend ESB, in the examples/talend/cxf folder, provides a dev-guide-java-first sample functionally equivalent to the dev-guide-wsdl-first example explored in the previous section. CXF's Java2ws tool, configured within the cxf-java2ws-plugin Maven plugin, is used for this process. For example, given a simple web service interface:

package service;

import javax.jws.WebService;
import javax.jws.WebMethod;

@WebService
public interface DoubleItPortType {
   public int doubleIt(int numberToDouble);
}

The code first developer will implement the web service, adding annotations to indicate desired web service configuration information:

package service;

import javax.jws.WebService;

@WebService(targetNamespace = "http://www.example.org/contract/DoubleIt",
            endpointInterface = "service.DoubleItPortType",
            serviceName = "DoubleItService",
            portName = "DoubleItPort")
public class DoubleItPortTypeImpl implements DoubleItPortType {

    public int doubleIt(int numberToDouble) {
        return numberToDouble * 2;
    }
}

If the Maven pom.xml has the cxf-java2ws-plugin configured as follows:

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-java2ws-plugin</artifactId>
    <version>${cxf.version}</version>
    <executions>
        <execution>
            <id>process-classes</id>
            <phase>process-classes</phase>
            <configuration>
                <className>service.DoubleItPortTypeImpl</className>
                <genWsdl>true</genWsdl>
                <verbose>true</verbose>
            </configuration>
            <goals>
                <goal>java2ws</goal>
            </goals>
        </execution>
    </executions>
</plugin>

An autogenerated two-part WSDL supporting this web service will be created, as shown below (certain areas truncated for brevity). The first file, DoubleItPortTypeImpl.wsdl contains message input and output information as well as the generic wsdl:portType that lists the method calls available. The wsdl:portType value incorporates the name of the web service interface. This interface also provides (from its doubleIt method) the name of the specific operation and its parameters.

<wsdl:definitions name="DoubleItPortType" targetNamespace="http://service/">
    <wsdl:types>
        <xs:schema elementFormDefault="unqualified" 
            targetNamespace="http://service/" version="1.0">
            <xs:element name="doubleIt" type="tns:doubleIt" />
            <xs:element name="doubleItResponse" type="tns:doubleItResponse" />
            <xs:complexType name="doubleIt">
                <xs:sequence>
                    <xs:element name="arg0" type="xs:int" />
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="doubleItResponse">
                <xs:sequence>
                    <xs:element name="return" type="xs:int" />
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="doubleIt">
        <wsdl:part name="parameters" element="ns1:doubleIt"/>
    </wsdl:message>
    <wsdl:message name="doubleItResponse">
        <wsdl:part name="parameters" element="ns1:doubleItResponse"/>
    </wsdl:message>
    <wsdl:portType name="DoubleItPortType">
        <wsdl:operation name="doubleIt">
            <wsdl:input name="doubleIt" message="ns1:doubleIt"/>
            <wsdl:output name="doubleItResponse" 
                message="ns1:doubleItResponse"/>
        </wsdl:operation>
    </wsdl:portType>
</wsdl:definitions>

The second file, DoubleItPortType.wsdl imports the former file and provides the explicit wsdl:binding and wsdl:service connection information. The wsdl:service incorporates the service name and port name values specified on the Java web service implementation above.

<wsdl:definitions name="DoubleItService" 
    targetNamespace="http://www.example.org/contract/DoubleIt">
    <wsdl:import namespace="http://service/" location="DoubleItPortType.wsdl"/>
    <wsdl:binding name="DoubleItServiceSoapBinding" 
        type="ns1:DoubleItPortType">
        <soap:binding style="document" 
            transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="doubleIt">
            <soap:operation soapAction="" style="document" />
            <wsdl:input name="doubleIt">
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output name="doubleItResponse">
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="DoubleItService">
        <wsdl:port name="DoubleItPort" 
            binding="tns:DoubleItServiceSoapBinding">
            <soap:address location="http://localhost:9090/DoubleItPort" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

This sample can be compiled and deployed just as explained in the previous wsdl-first example--only difference, for Talend ESB OSGi deployment, to use install mvn:org.talend.cxf-examples.dev-guide-java-first/dev-guide-java-first-service/ as the bundle install string.

In the next Java-first example we'll demonstrate other helpful tools for web services development, including the m2eclipse plugin, Apache CXF's Maven archetypes for creating skeleton code and soapUI for making test SOAP calls.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!