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:

4 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes