Java API for XML Web services(JAX-WS) is one of the importance feature of Java Platform, Standard Edition 6 ( Java SE 6).
Java 6 is bundled with all libraries which are needed to compile, execute and consume Java Web Services. JAX-WS supports both SOAP and Rest based web services.
In this post we learn how to create simple JAX-WS based webservices using Service Endpoint Interface( SEI). SEI declares the methods which are the web service operations.
To start with the sample application development, the following software need to be install on your computer.
- Eclipse (I have used Eclipse Indigo for this tutorial).
- JDK 1.6. As mentioned Jdk 6 has all libraries which are useful to develope SOAP based Web services.
1. Create a Java Project in the Eclipse.:
Click on File -> New Porject -> Java Project.
Enter the project name as JaxWsProject. Select JRE version as JavaSE-1.6 as mentioned in the below screen shot. Click on Finish button.
2. Create the Webservice End point Interface.
In this tutorial we will create a Temparature converter Webservice which as two methods.
farenheitToCelcius -- This method convert the Farenheit value to Celicius.
CelciusToFarenheit -- This method convert the Celcius value to Farenheit.
Select the
JaxWsProject on the Package Explorer window.
Right click on src folder and Select
New -> Interface.
Enter package name as "com.mycompany.service".
Enter Interface name as "Converter".
To publish a class as a web service class should be annotated with @WebService and methods should be annotated with @WebMethod
@WebService signals that it is SEI(Service endpoing interface).
@WebMethod signals that annotated method is a web operation.
Replace the interface content with the code given below.
As mentioned aboe, Converter Interface is Annoted with @WebService and methods are
Annoted with @WebMethod.
package com.mycompany.service;
import javax.jws.WebService;
import javax.jws.WebMethod;
@WebService
public interface Converter {
@WebMethod
double farenheitToCelcius(double farenheit);
@WebMethod
double celciusToFarenheit(double celcius);
}
3. Create the Web Service Implementation.
Let start with create a class which will implement the Converter Interfaces listed above.
Select the JaxWsProject node in the package Explorer.
Right Click on the src node select Class.
Enter package name as "com.mycompany.service".
Enter Class name as "ConverterImpl".
Click on Finish.
Replace the class content with the code given below. The ConverterImpl class will implement the Converter interface which is declared as aweb service by annotating the interface with @WebService annotation.
As you can observe in the below code snippet ConverterImpl class also annoted with @WebService. In addition to that enpointInterface paramenter is provided which has value of full qualified name of the Converter Interface.
And no need to provide any annotation for the overriden method implementation.
package com.mycompany.service;
import javax.jws.WebService;
@WebService (endpointInterface = "com.mycompany.service.Converter")
public class ConverterImpl implements Converter {
@Override
public double farenheitToCelcius(double farenheit) {
return (farenheit - 32) * 5 / 9;
}
@Override
public double celciusToFarenheit(double celcius) {
return celcius * 9 / 5 + 32;
}
}
4. WSGEN Tool:
Before Publisher publish the web service or to generate the WSDL file, it need some java artifacts.
wsgen utility which is part of JDK 1.6 will generate these required java artifacts. wsgen utility can be seen at %JAVA_HOME%/bin path.
To run the Wsgen tool , Open the command window and navigate to the JaxWsProject project folder.
Navaigate to the bin folder of the JaxWsProject.
Run the below command.
-cp <path> specify where to find input class files.
-s <directory> specify where to place generated source files
-verbose output messages about what the compiler is doing
-keep keep generated files
This command will generate java files in
com.mycompany.service.jaxws package.
Create a folder named wsdl under src directory and run the below command in the bin directory. This will generate wsdl file in the swdl folder.
-r <directory> - resource destination directory, specify where to place resouce files such as WSDLs
-wsdl - generate a WSDL file.
5. Publish the Web Service:
Create a ConverterPublisher class with content given below.
package com.mycompany.endpoint;
import javax.xml.ws.Endpoint;
import com.mycompany.service.ConverterImpl;
public class ConverterPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:9996/ws/Converter",
new ConverterImpl());
System.out.println(" Converter Service is published successfully");
}
}
Run the ConverterPublisher class, to deploy the Converter Webservice at "http://localhost:9996/ws/Converter". You can access the wsdl file with http://localhost:9996/ws/Converter?wsdl
6. Converter WSDL
The Generated Converter Webservice wsdl file is given below.
xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<
definitions targetNamespace="http://service.mycompany.com/" name="ConverterImplService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.mycompany.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<types>
<xsd:schema>
<xsd:import namespace="http://service.mycompany.com/" schemaLocation="ConverterImplService_schema1.xsd"/>
</xsd:schema>
</types>
<message name="farenheitToCelcius">
<part name="parameters" element="tns:farenheitToCelcius"/>
</message>
<message name="farenheitToCelciusResponse">
<part name="parameters" element="tns:farenheitToCelciusResponse"/>
</message>
<message name="celciusToFarenheit">
<part name="parameters" element="tns:celciusToFarenheit"/>
</message>
<message name="celciusToFarenheitResponse">
<part name="parameters" element="tns:celciusToFarenheitResponse"/>
</message>
<portType name="Converter">
<operation name="farenheitToCelcius">
<input message="tns:farenheitToCelcius"/>
<output message="tns:farenheitToCelciusResponse"/>
</operation>
<operation name="celciusToFarenheit">
<input message="tns:celciusToFarenheit"/>
<output message="tns:celciusToFarenheitResponse"/>
</operation>
</portType>
<binding name="ConverterImplPortBinding" type="tns:Converter">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="farenheitToCelcius">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="celciusToFarenheit">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="ConverterImplService">
<port name="ConverterImplPort" binding="tns:ConverterImplPortBinding">
<soap:address location="REPLACE_WITH_ACTUAL_URL"/>
</port>
</
</service>definitions>
<?