Wednesday, November 20, 2013

Building a contract-first webservice with JAX-WS

In this tutorial, we'll be building a webservice contract-first. This means we build the webservice based on a contract that already exists. This contract is the WSDL file that describes the webservice formally.

The first step is to generate JAX-WS classes using wsgen that comes with the Java EE SDK. You can find the executable in your SDK/Glassfish installation directory. We'll be using the WSDL file of an earlier blogpost. Save this file as: ExampleWSService.wsdl. If you're using Eclipse, you might want to save the files in WebContent/WEB-INF/wsdl in the project directory.

  1. xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <definitions targetNamespace="http://javaeenotes.com/"  
  3. name="ExampleWSService"  
  4. xmlns="http://schemas.xmlsoap.org/wsdl/"  
  5. xmlns:tns="http://javaeenotes.com/"  
  6. xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  7. xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">  
  8.   <types>  
  9.     <xsd:schema>  
  10.       <xsd:import namespace="http://javaeenotes.com/" schemaLocation="ExampleWSService.xsd"/>  
  11.     </xsd:schema>  
  12.   </types>  
  13.   <message name="sum">  
  14.     <part name="parameters" element="tns:sum"/>  
  15.   </message>  
  16.   <message name="sumResponse">  
  17.     <part name="parameters" element="tns:sumResponse"/>  
  18.   </message>  
  19.   <message name="greet">  
  20.     <part name="parameters" element="tns:greet"/>  
  21.   </message>  
  22.   <message name="greetResponse">  
  23.     <part name="parameters" element="tns:greetResponse"/>  
  24.   </message>  
  25.   <message name="multiply">  
  26.     <part name="parameters" element="tns:multiply"/>  
  27.   </message>  
  28.   <message name="multiplyResponse">  
  29.     <part name="parameters" element="tns:multiplyResponse"/>  
  30.   </message>  
  31.   <portType name="ExampleWS">  
  32.     <operation name="sum">  
  33.       <input message="tns:sum"/>  
  34.       <output message="tns:sumResponse"/>  
  35.     </operation>  
  36.     <operation name="greet">  
  37.       <input message="tns:greet"/>  
  38.       <output message="tns:greetResponse"/>  
  39.     </operation>  
  40.     <operation name="multiply">  
  41.       <input message="tns:multiply"/>  
  42.       <output message="tns:multiplyResponse"/>  
  43.     </operation>  
  44.   </portType>  
  45.   <binding name="ExampleWSPortBinding" type="tns:ExampleWS">  
  46.     <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>  
  47.     <operation name="sum">  
  48.       <soap:operation soapAction=""/>  
  49.       <input>  
  50.         <soap:body use="literal"/>  
  51.       </input>  
  52.       <output>  
  53.         <soap:body use="literal"/>  
  54.       </output>  
  55.     </operation>  
  56.     <operation name="greet">  
  57.       <soap:operation soapAction=""/>  
  58.       <input>  
  59.         <soap:body use="literal"/>  
  60.       </input>  
  61.       <output>  
  62.         <soap:body use="literal"/>  
  63.       </output>  
  64.     </operation>  
  65.     <operation name="multiply">  
  66.       <soap:operation soapAction=""/>  
  67.       <input>  
  68.         <soap:body use="literal"/>  
  69.       </input>  
  70.       <output>  
  71.         <soap:body use="literal"/>  
  72.       </output>  
  73.     </operation>  
  74.   </binding>  
  75.   <service name="ExampleWSService">  
  76.     <port name="ExampleWSPort" binding="tns:ExampleWSPortBinding">  
  77.       <soap:address location="http://127.0.0.1:8080/webservice/ExampleWSService"/>  
  78.     </port>  
  79.   </service>  
  80. </definitions>  


This WSDL file imports an XSD file. Make sure this is placed in the same directory as the WSDL file, and name it ExampleWSService.xsd. Also update the location/URL in the WSDL file. This example assumes the application is called webservice and the service name is ExampleWSService.

  1. xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <xs:schema version="1.0"  
  3. targetNamespace="http://javaeenotes.com/"  
  4. xmlns:tns="http://javaeenotes.com/"  
  5. xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  6.   
  7.   <xs:element name="greet" type="tns:greet"/>  
  8.   
  9.   <xs:element name="greetResponse" type="tns:greetResponse"/>  
  10.   
  11.   <xs:element name="multiply" type="tns:multiply"/>  
  12.   
  13.   <xs:element name="multiplyResponse" type="tns:multiplyResponse"/>  
  14.   
  15.   <xs:element name="sum" type="tns:sum"/>  
  16.   
  17.   <xs:element name="sumResponse" type="tns:sumResponse"/>  
  18.   
  19.   <xs:complexType name="greet">  
  20.     <xs:sequence>  
  21.       <xs:element name="arg0" type="xs:string" minOccurs="0"/>  
  22.     </xs:sequence>  
  23.   </xs:complexType>  
  24.   
  25.   <xs:complexType name="greetResponse">  
  26.     <xs:sequence>  
  27.       <xs:element name="return" type="xs:string" minOccurs="0"/>  
  28.     </xs:sequence>  
  29.   </xs:complexType>  
  30.   
  31.   <xs:complexType name="sum">  
  32.     <xs:sequence>  
  33.       <xs:element name="arg0" type="xs:int"/>  
  34.       <xs:element name="arg1" type="xs:int"/>  
  35.     </xs:sequence>  
  36.   </xs:complexType>  
  37.   
  38.   <xs:complexType name="sumResponse">  
  39.     <xs:sequence>  
  40.       <xs:element name="return" type="xs:int"/>  
  41.     </xs:sequence>  
  42.   </xs:complexType>  
  43.   
  44.   <xs:complexType name="multiply">  
  45.     <xs:sequence>  
  46.       <xs:element name="arg0" type="xs:int"/>  
  47.       <xs:element name="arg1" type="xs:int"/>  
  48.     </xs:sequence>  
  49.   </xs:complexType>  
  50.   
  51.   <xs:complexType name="multiplyResponse">  
  52.     <xs:sequence>  
  53.       <xs:element name="return" type="xs:int"/>  
  54.     </xs:sequence>  
  55.   </xs:complexType>  
  56. </xs:schema>  


Now use the wsgen command to generate the server stubs. I generally put the complete command in a Windows batch file, so I can easily rerun the command in the future if necessary. Now, execute the following command:

wsimport -p com.javaeenotes.ws -d build/classes -s src WebContent/WEB-INF/wsdl/ExampleWSService.wsdl

This will generate all classes needed to setup the webservice. Next is to create a class that implements the webservice.

  1. package com.javaeenotes;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. import com.javaeenotes.ws.ExampleWS;  
  6.   
  7. @WebService(name = "ERSService",  
  8.         targetNamespace = "http://javaeenotes.com/",  
  9.         serviceName = "ExampleWSService",  
  10.         portName = "ExampleWSPort",  
  11.         endpointInterface = "com.javaeenotes.ws.ExampleWS",  
  12.         wsdlLocation = "WEB-INF/wsdl/ExampleWSService.wsdl")  
  13. public class WsImplementation implements ExampleWS {  
  14.   
  15.     @Override  
  16.     public int sum(int arg0, int arg1) {  
  17.         return 0;  
  18.     }  
  19.   
  20.     @Override  
  21.     public String greet(String arg0) {  
  22.         return null;  
  23.     }  
  24.   
  25.     @Override  
  26.     public int multiply(int arg0, int arg1) {  
  27.         return 0;  
  28.     }  
  29. }  


This class implements the webservice interface we generated from the WSDL file. Using simple annotations, we can easily deploy it without editting any deployment descriptors. Nothing more is needed for successful deployment. If you used the default URL in the WSDL file, you can now reach the webservice using this URL:

http://127.0.0.1:8080/webservice/ExampleWSService?wsdl

I recommend SOAP-UI to test the webservice.

1 comment:

No comments: