The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services. It is used to develop Web services and is a part of the Sun Java development kit (JDK). JAX-WS technology is used with other technologies, either from the core group or more enhanced Web services.
- JAX-WS was designed to replace the existing JAX-RPC (remote procedure call). The name was changed to JAX-WS from JAX-RPC to reflect the shift from RPC-style to document-style Web services.
- JAX-WS consists of a standardized set of extensions for Java, which enable the development of Java-based Web services through WSDL. Just like JAX-RPC, JAX-WS also uses SOAP to represent a RPC. SOAP includes specifications, encoding rules, important structures, corresponding responses and necessary conventions to do RPCs over the network.
- JAX-WS makes use of annotations to keep the development and deployment of Web service clients and endpoints simple. It is also known as JAX-WS RI, which means JAX-WS for reference implementation, and is now a part of metro distribution. The reference implementation of JAX-WS is designed as an open-source project, which is a part of GlassFish -- an open-source Java application server. This definition was written in the context of JAX-WS.
Developing a Service using JAX-WS
You can develop a service using one of two approaches:- Start with a WSDL contract and generate Java objects to implement the service.
- Start with a Java object and service enable it using annotations.
For new development the preferred path is to design your services in WSDL and then generate the code to implement them. This approach enforces the concept that a service is an abstract entity that is implementation neutral. It also means you can spend more time working out the exact interface your service requires before you start coding.
WSDL First Development
Using the WSDL first model of service development, you start with a WSDL document that defines the service you wish to implement. This WSDL document could be obtained from another developer, a system architect, a UDDI registry, or you could write it yourself. The document must contain at least a fully specified logical interface before you can begin generating code from it.Once you have a WSDL document, the process for developing a JAX-WS service is three steps:
- Generate starting point code.
- Implement the service's operations.
- Publish the implemented service.
Generating the Starting Point Code
JAX-WS specifies a detailed mapping from a service defined in WSDL to the Java classes that will implement that service. The logical interface, defined by the wsdl:portType element, is mapped to a service endpoint interface (SEI). Any complex types defined in the WSDL are mapped into Java classes following the mapping defined by the Java Architecture for XML Binding (JAXB) specification. The endpoint defined by the wsdl:service element is also generated into a Java class that is used by consumers to access endpoints implementing the service.The wsdl2java command automates the generation of this code. It also provides options for generating starting point code for your implementation and an ant based makefile to build the application. wsdl2java provides a number of arguments for controlling the generated code.
Running wsdl2java can be done in two ways :-
1. Using Command Prompt.
You can generate the code needed to develop your service using the following command:wsdl2java -ant -impl -server -d outputDir myService.wsdl
The command does the following:
- The -ant argument generates a Ant makefile, called build.xml, for your application.
- The -impl argument generates a shell implementation class for each portType element in the WSDL document.
- The -server argument generates a simple main() to launch your service as a stand alone application.
- The -d outputDir argument tells wsdl2java to write the generated code to a directory called outputDir.
- myService.wsdl is the WSDL document from which code is generated.
Create a project and install ANT Build tool in it.
http://ant.apache.org/manual/install.html
Use the below script to generate WSDLtoJava.
<?xml version="1.0"?>
<project name="cxf wsdl2java" basedir=".">
<property name="cxf.home" location ="C:/Data/Technical/apache-cxf-2.6.1"/>
<path id="cxf.classpath">
<fileset dir="${cxf.home}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="cxfWSDLToJava">
<java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">
//Use this for generating Client code
<arg value="-client"/>
//Use this for generating Server code
<arg value="-server"/>
<arg value="-d"/>
<arg value="client"/>
<arg value="http://localhost:9080/CXFBasic/ChangeStudent?wsdl"/>
<classpath>
<path refid="cxf.classpath"/>
</classpath>
</java>
</target>
</project>