This tutorial shows you how to write contract-first Web services, as there have two ways to develop a web service, start with the XML Schema/WSDL contract first followed by the Java code second, or start with Java code first followed by WSDL second, the preferred method to developers is to use contract-first mode, and then use the wsdl2java utility to generate the Java stub code from the WSDL contract.
The WSDL defines port type, with a SOAP binding and service and SOAPService, the first thing we goes is to develop WSDL, that allows to design the approach/solution as best as possible, then create a build task (ant: use command line wsdl2java in ant) that generates the code and packages. When the wsdl changes, you only have to re-run the build task to regenerate code.
Using CXF wsdl2java tool we need import the following jars to classpath.
<file name=”jaxb-xjc.jar”/><!– dependency of cxf –>
<file name=”velocity.jar”/><!– dependency of cxf –>
<file name=”aopalliance.jar”/><!– dependency of cxf –>
<file name=”asm.jar”/><!– dependency of cxf –>
<file name=”cxf.jar”/>
<file name=”FastInfoset.jar”/><!– dependency of cxf –>
<file name=”geronimo-activation_1.1_spec.jar”/><!– dependency of cxf –>
<file name=”geronimo-annotation_1.0_spec.jar”/><!– dependency of cxf –>
<file name=”geronimo-javamail_1.4_spec.jar”/><!– dependency of cxf –>
<file name=”geronimo-jaxws_2.1_spec.jar”/><!– dependency of cxf –>
<file name=”geronimo-stax-api_1.0_spec.jar”/><!– dependency of cxf –>
<file name=”geronimo-ws-metadata_2.0_spec.jar”/><!– dependency of cxf –>
<file name=”jra.jar”/><!– dependency of cxf –>
<file name=”neethi.jar”/><!– dependency of cxf –>
<file name=”saaj-api.jar”/><!– dependency of cxf –>
<file name=”saaj-impl.jar”/><!– dependency of cxf –>
<file name=”spring-core.jar”/><!– dependency of cxf –>
<file name=”spring-beans.jar”/><!– dependency of cxf –>
<file name=”spring-context.jar”/><!– dependency of cxf –>
<file name=”wsdl4j.jar”/><!– dependency of neethi –>
<file name=”wstx-asl.jar”/><!– dependency of cxf –>
<file name=”xmlschema.jar”/><!– dependency of cxf –>
This is an example that takes a WSDL document and generates fully annotated Java code from which to implement a service.
<target name="generate_wsdl" depends="init"> <mkdir dir="${build.gen}"/> <java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true"> <arg value="-d"/> <arg value="${build.gen}"/> <arg value="-p"/> <arg value="com.asjava> <arg value="-ant"/> <arg value="${main.res}/asjava.wsdl"/> <classpath> <path refid="main.cp"/> </classpath> </java> </target>
${build.gen}
specifics the folder which new sources will be outputted in.
${main.res}/asjava.wsdl
specifics the WSDL we used to generate to Java sources.
main.cp
is the classpath defined and used in this ant.
<arg value="com.asjava>
specifics the package name to organize these newly generated sources. now you may follow this cxf wsdl2java example to have a try.