Listing 1: MovieService.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions . . . >
   <wsdl:types>
      <xsd:schema targetNamespace="http://www.example.org/movieservice/">
         <xsd:import namespace="http://www.example.org/movieservice/type/"
		    schemaLocation="MovieServiceTypes.xsd"/>
      </xsd:schema>
   </wsdl:types>

   <wsdl:message name="GetShowtimeRequest">
      <wsdl:part element="tnstype:zip" name="zip"/>
   </wsdl:message>
   <wsdl:message name="GetShowtimeResponse">
      <wsdl:part element="tnstype:theaters" name="theaters"/>
   </wsdl:message>
	
   <wsdl:portType name="MovieService">
      <wsdl:operation name="getShowtimeForZip">
         <wsdl:input message="tns:GetShowtimeRequest"/>
         <wsdl:output message="tns:GetShowtimeResponse"/>
      </wsdl:operation>
   </wsdl:portType>

   <wsdl:binding name="MovieServiceSOAP" type="tns:MovieService">
      <soap:binding style="document" 
	      transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="getShowtimeForZip">
         <soap:operation style="document"/>
         <wsdl:input><soap:body use="literal"/></wsdl:input>
         <wsdl:output><soap:body use="literal"/></wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
	
   <wsdl:service name="MovieService">
      <wsdl:port binding="tns:MovieServiceSOAP" name="MovieServiceSOAP">
         <soap:address 
             location="http://localhost:8080/MovieService/cxf/MovieService"/>
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

Listing 2: Running wsdl2java from command line

wsdl2java MovieService.wsdl

Listing 3: Specifying the package

wsdl2java -p com.example.cxf.generatedcode MovieService.wsdl

Listing 4: Specifying a binding file

wsdl2java HelloWorld.wsdl -b my_binding.xml

Listing 5: Generating Ant build file

wsdl2java -ant MovieService.wsdl

Listing 6: wsdl2java macro definition

<macrodef name="wsdl2java">
   <attribute name="srcdestdir" default="${wsdl2java.src.dest.dir}"/>
   <attribute name="destdir" default="${wsdl2java.bin.dest.dir}"/>
   <attribute name="file"/>
   <attribute name="servicename.arg" default=""/>
   <attribute name="bindingfile" default=""/>
   <attribute name="dir" default="${wsdl.dir}"/>
   <attribute name="package" default="NOT_SPECIFIED"/>
   <sequential>
      <condition property="package.arg.@{file}" value="-p @{package}">
         <not>
            <equals arg1="@{package}" arg2="NOT_SPECIFIED"/>
         </not>
      </condition>
      <property name="package.arg.@{file}" value=""/>
      <condition property="binding.arg" value='-b "@{bindingfile}"'>
         <not>
            <equals arg1="@{bindingfile}" arg2=""/>
         </not>
      </condition>
      <property name="binding.arg" value=""/>
      <java failonerror="true" 
            classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="yes">
         <classpath>
            <path refid="cxf.classpath" />
         </classpath>
         <sysproperty key="exitOnFinish" value="true"/>
         <arg line="@{servicename.arg}"/>
         <arg line="${package.arg.@{file}}"/>
         <arg line="${binding.arg}"/>
         <arg value="-verbose"/>
         <arg value="-d"/>
         <arg value="@{srcdestdir}"/>
         <arg value="@{dir}/@{file}"/>
      </java>
   </sequential>
</macrodef>

Listing 7: Calling wsdl2java macro

<target name="wsdl.to.java">
   <wsdl2java file="MovieService.wsdl"/>
</target>

Listing 8: Service implementation class

@WebService(endpointInterface = "org.example.movieservice.MovieService")
public class MovieServiceImpl implements MovieService {
    public TheaterListType getShowtimeForZip(String zip) {
        // Code ignored
    }
}

Listing 9: JAX-WS Dynamic Web Service Publishing

import javax.xml.ws.Endpoint;

MovieServiceImpl implementor = new MovieServiceImpl();
String address = "http://localhost:8080/MyMovieService";
Endpoint.publish(address, implementor);

Listing 10: Spring configuration in web.xml

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>WEB-INF/cxf-bean.xml</param-value>
</context-param>

<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>

Listing 11: Servlet configuration in web.xml

<servlet>
   <servlet-name>CXFServlet</servlet-name>
   <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>CXFServlet</servlet-name>
   <url-pattern>/cxf/*</url-pattern>
</servlet-mapping>

Listing 12: Spring configuration file cxf-bean.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    
    <jaxws:endpoint 
	  id="MovieServiceEndpointBean" 
	  implementor="org.example.movieservice.MovieServiceImpl" 
	  address="/MovieService" />
</beans>

Listing 13: Example JAX-WS client

MovieService_Service service = new MovieService_Service();
MovieService port = service.getMovieServiceSOAP();  
TheaterListType list = port.getShowtimeForZip("91000");

Listing 14: RESTful service using CXF annotations

import org.codehaus.jra.Delete;
import org.codehaus.jra.Get;
import org.codehaus.jra.HttpResource;
import org.codehaus.jra.Post;
import org.codehaus.jra.Put;

@WebService
public interface TicketBooking {
    @Post
    @HttpResource(location="/tickets")
    public void bookTicket(Ticket ticket);

    @Get
    @HttpResource(location="/tickets/{ticketID}")
    public Ticket getTicket(long ticketId);

    @Put
    @HttpResource(location="/tickets/{ticketID}")
    public void updateTicketOrder(Ticket ticket, long ticketID);

    @Delete
    @HttpResource(location="/tickets/{ticketID}")
    public void cancelBooking(long ticketID);
}

Listing 15: Using Provider to modify the XML message

import java.io.InputStream;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Provider;
import javax.xml.ws.Service;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceProvider;

@WebServiceProvider(portName = "SoapPort", 
                    serviceName = "SoapService",
                    targetNamespace = "http://example.org/tns",
                    wsdlLocation = "wsdl/example.wsdl")
@ServiceMode(value = Service.Mode.MESSAGE)
public class MyProvider implements Provider<SOAPMessage> {
    public MyProvider () {
    }

    public SOAPMessage invoke(SOAPMessage request) {
        SOAPMessage response = null;
        // 
        // Code ingored
        //
        return response;
    }
}

Listing 16 JaxWsProxyFactory bean configuration

<beans . . .>
   <bean id="movieServiceClient" class="org.example.movieservice.MovieService"
         factory-bean="clientFactory" factory-method="create"/>
      <bean id="clientFactory" 
            class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
         <property name="serviceClass" 
                   value="org.example.movieservice.MovieService"/>
         <property name="address" 
                   value="http://localhost:8080/MovieService/cxf/MovieService"/>
   </bean>
</beans>

Listing 17 Get the client bean from Spring context

ApplicationContext context = new ApplicationContext(. . .);
MovieService client = (MovieService)context.getBean("movieService
Client");