Webservices

JAXWS Webservice Example
Author : Ishtek


This is page I shall show a step by step process of developing a basic example 
Webservice using JAX-WS, and Eclipse as IDE and JBoss 7.1.1 as application server.






I have used 'Eclipse Java EE IDE for Web Developers', version Juno,
and JBoss Application Server 7.1.1, and JDK 1.7.0_15.

I shall be using JAX-WS 2.0 and annotations from JDK 1.7 API, such as
@javax.jws.WebService and @javax.jws.WebMethod etc.

This example web service server side code could be as simple as a POJO
with a single method that return a String, as shown below:
TestServiceImpl.java
package net.techiecommunity; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class TestServiceImpl { @WebMethod public String getTestName() { return "Ishtek"; } }
Here I have used two annotations, one at the class level and another at the method level. Class level annotation @WebService denotes that this class is a web service endpoint implementation. And the method level annotation @WebMethod denotes that this method getTestName is exposed as the operation in the WSDL file that will be created automatically, as I have choosen 'Bottom up Java Bean web service' approach (refering to wizard used in the Eclipse web service creation ). In this approach, service implementation is coded first, and then this bean can used to create web service. The other approach could be 'Top down Java Bean service', where the service definition (in form of WSDL file) is used to create web service. I have followed following steps to setup this example and create a simple example web service: 1. Create an Enterprise Application Project (EAR type project). 2. Create a dynamic web project, and select the EAR project that is created in the previous step. 3. Create a class with an appropriate package name and use @WebService annotations to demarcate this class as a web service implementation class file. 4. Provide annotation for the method as @WebMethod for marking this method as the endpoint operation. 5. Create an EAR file for the Enterprise application project, by using export option from within Eclipse menu. 6. I have used JBoss Application server 7.1.1 standalone mode, for this mode, I have placed the final EAR file under %JBOSS_HOME%/standalone/deployments folder. 7. Started application server by using %JBOSS_HOME%/bin/standalone.bat file. 8. If you can observe output on JBoss server console, something like as follows:
...JBAS015876: Starting deployment of "SampleWSApp.ear" ...JBAS015876: Starting deployment of "SampleWeb.war" ... Add Service id=net.techiecommunity.TestServiceImpl address=http://localhost:8080/SampleWeb/TestServiceImpl implementor=net.techiecommunity.TestServiceImpl invoker=org.jboss.wsf.stack.cxf.JBossWSInvoker serviceName={http://techiecommunity.net/}TestServiceImplService portName={http://techiecommunity.net/}TestServiceImplPort wsdlLocation=null mtomEnabled=false .. Creating Service {http://techiecommunity.net/}TestServiceImpl .. Setting the server's publish address to be http://localhost:8080/SampleWeb/TestService .. WSDL published to: file:/D:/jboss-as-7.1.1.Final/standalone/data/wsdl .. Starting service jboss.ws.endpoint."SampleWSApp.ear"."SampleWeb.war"."net.techiecomm .. register: jboss.ws:context=SampleWeb,endpoint=net.techiecommunity. .. Registering web context: /SampleWeb .. Deployed "SampleWSApp.ear"
Then there can be another way to check to see whether web service is registered properly or not. by using a URL as http://localhost:8080/SampleWeb/TestServiceImpl?wsdl, if the response is the WSDL file content on browser screen. And one can check by using JBoss Management Admin console http://localhost:8080/console -> Deployments -> Webservices. Under this option, there should be web service endpoint getting reflected. In order to test this deployed web service, I have used SOAPUI version 4.0. Using the WSDL URL http://localhost:8080/SampleWeb/TestServiceImpl?wsdl, I have created a project for testing response as it returned for the default request. This example WSDL file is automatically created while deploying the EAR file in JBoss Application Server, and it looks something like as follows:
<wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://techiecommunity.net/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TestServiceImplService" targetNamespace="http://techiecommunity.net/"> <wsdl:types> <xs:schema xmlns:tns="http://techiecommunity.net/" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://techiecommunity.net/" version="1.0"> <xs:element name="getTestName" type="tns:getTestName"/> <xs:element name="getTestNameResponse" type="tns:getTestNameResponse"/> <xs:complexType name="getTestName"> <xs:sequence/> </xs:complexType> <xs:complexType name="getTestNameResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="getTestName"> <wsdl:part element="tns:getTestName" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="getTestNameResponse"> <wsdl:part element="tns:getTestNameResponse" name="parameters"></wsdl:part> </wsdl:message> <wsdl:portType name="TestServiceImpl"> <wsdl:operation name="getTestName"> <wsdl:input message="tns:getTestName" name="getTestName"></wsdl:input> <wsdl:output message="tns:getTestNameResponse" name="getTestNameResponse"></wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="TestServiceImplServiceSoapBinding" type="tns:TestServiceImpl"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="getTestName"> <soap:operation soapAction="" style="document"/> <wsdl:input name="getTestName"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="getTestNameResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="TestServiceImplService"> <wsdl:port binding="tns:TestServiceImplServiceSoapBinding" name="TestServiceImplPort"> <soap:address location="http://localhost:8080/SampleWeb/TestServiceImpl"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
For this example, SOAP Request and Response are as follows: SOAP Request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tec="http://techiecommunity.net/"> <soapenv:Header/> <soapenv:Body> <tec:getTestName/> </soapenv:Body> </soapenv:Envelope>
SOAP Response
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:getTestNameResponse xmlns:ns2="http://techiecommunity.net/"> <return>Ishtek</return> </ns2:getTestNameResponse> </soap:Body> </soap:Envelope>
This is reponse is correct as per this example code. Following section added as of 21-Dec-2013 Let me show you a step by step way, how I have deployed a Webservice using 'Contract-First' approach, for example, I have created a WSDL file using notepad++ as editor, and then I have used this WSDL file with wsimport command from JDK 1.7.0_15 version, to create necessary Java/class files. To start with I have written a WSDL and and XSD files as shown below: WSDL file is depicting a webservice with name as "EmployeeManagementWebservice" and the XSD schema file is depicting custom data type used in request and response parameters to the webmethod/operation "getEmployeeDetails". To this operation, argument is an object type of EmployeeManagementRequest, and return type of this method is object of EmployeeManagementResponse class type. Folder structure of the web project is as follows: EmployeeManagementWeb -> WebContent -> WEB-INF -> wsdl -> EmployeeManagement_WSDL.xml
<?xml version="1.0" encoding="UTF-8" ?> <definitions targetNamespace="http://techiecommunity.net/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://techiecommunity.net/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> <types> <xsd:schema> <xsd:import namespace="http://techiecommunity.net/" schemaLocation="EmployeeManagement_schema.xsd"/> </xsd:schema> </types> <message name="EmployeeManagementRequestMsg"> <part name="parameters" element="tns:EmployeeManagementRequest"/> </message> <message name="EmployeeManagementResponseMsg"> <part name="parameters" element="tns:EmployeeManagementResponse"/> </message> <portType name="EmployeeManagementWebservice"> <operation name="getEmployeeDetails"> <input message="tns:EmployeeManagementRequestMsg"/> <output message="tns:EmployeeManagementResponseMsg"/> </operation> </portType> <binding name="EmployeeManagementWebservicePortBinding" type="ns1:EmployeeManagementWebservice" xmlns:ns1="http://techiecommunity.net/"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="getEmployeeDetails"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="EmployeeManagementWebservice"> <port name="EmployeeManagementWebservicePort" binding="tns:EmployeeManagementWebservicePortBinding"> <soap:address location="http://localhost:8080/EmployeeManagementWeb/EmployeeManagementWebservice"/> </port> </service> </definitions>
Above WSDL file has a XSD file defined for the custom data type used in this example. EmployeeManagement_schema.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema version="1.0" targetNamespace="http://techiecommunity.net/" xmlns:tns="http://techiecommunity.net/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="EmployeeManagementRequest" type="tns:EmployeeManagementRequest"/> <xs:element name="EmployeeManagementResponse" type="tns:EmployeeManagementResponse"/> <xs:complexType name="EmployeeManagementRequest"> <xs:sequence/> </xs:complexType> <xs:complexType name="EmployeeManagementResponse"> <xs:sequence> <xs:element name="return" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:schema>
And following is the command I used for generated Java files and class files.
wsimport -d ../../../src EmployeeManagement_WSDL.xml -keep
There will be six Java files auto-generated and I received compilation error from an autogenerated file "EmployeeManagementWebservice_Service.java" from within Eclipse IDE. On opening this file, it seems to me that this file can be ignored, so I took a backup of this Autogenerated Java file, and moved it out of SRC folder. Finally I have used Five Java files those are autogenerated by wsimport tool from JDK version I have used is 1.7.0_15.
  • EmployeeManagementRequest.java
  • EmployeeManagementResponse.java
  • EmployeeManagementWebservice.java
  • ObjectFactory.java
  • package-info.java
  • As per the WSDl file, the package name for these Java files is 'net.techiecommunity'. As the service is an interface, so the service implementation should be a class that implements this interface 'EmployeeManagementWebservice'. EmployeeManagementWebserviceImpl.java
    package net.techiecommunity; import javax.jws.WebParam; import javax.jws.WebService; @WebService(serviceName = "EmployeeManagementWebservice", targetNamespace = "http://techiecommunity.net/", portName = "EmployeeManagementWebservicePort", wsdlLocation = "/WEB-INF/wsdl/EmployeeManagement_WSDL.xml", endpointInterface = "net.techiecommunity.EmployeeManagementWebservice") public class EmployeeManagementWebserviceImpl implements EmployeeManagementWebservice { @Override public EmployeeManagementResponse getEmployeeDetails( @WebParam(name = "EmployeeManagementRequest", targetNamespace = "http://techiecommunity.net/", partName = "parameters") EmployeeManagementRequest parameters) { EmployeeManagementResponse response = new EmployeeManagementResponse(); response.setReturn("Test"); return response; } }
    Bundle this web application in a Enterprise Application Project (EAR type packaged) and deploy this onto the JBoss Application Server (I have used 7.1.1 version in this example). After starting JBoss Application Server 7.1.1 in standalone mode and observe following on console output:
    ... Starting deployment of "EmployeeManagementEAR.ear" ... Starting deployment of "EmployeeManagementWeb.war" ... Add Service id=net.techiecommunity.EmployeeManagementWebserviceImpl address=http://localhost:8080/EmployeeManagementWeb/EmployeeManagementWebservice implementor=net.techiecommunity.EmployeeManagementWebserviceImpl invoker=org.jboss.wsf.stack.cxf.JBossWSInvoker serviceName={http://techiecommunity.net/}EmployeeManagementWebservice portName={http://techiecommunity.net/}EmployeeManagementWebservicePort wsdlLocation=null mtomEnabled=false ... Creating Service {http://techieco ... Setting the server's publish address to be http://localhos ... WSDL published to: file:/D:/jboss-as-7.1 ... Starting service jboss.ws.port-component-link ... Starting service jboss.ws.endpoint."EmployeeManagementEA ... register: jboss.ws:context=EmployeeMan ... /EmployeeManagementWeb ... Admin console listening on http://127.0.0.1:9990 ... JBoss AS 7.1.1.Final "Brontes" started in 174406ms - Started 189 of ... Deployed "EmployeeManagementEAR.ear"
    Now I did check whether this webservice is deployed successully and functioning well, by checking in the Management web console @ http://localhost:8080/console and by using URL of the WSDL @ http://localhost:8080/EmployeeManagementWeb/EmployeeManagementWebservice?wsdl by receiving browser response as the WSDL file content only (with slight modification in some ways, including the way XSD schema content is accessed). And then I used SOAPUI for checking out request and response in SOAP format, is shown as follows: SOAPUI - SOAP Request
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tec="http://techiecommunity.net/"> <soapenv:Header/> <soapenv:Body> <tec:EmployeeManagementRequest/> </soapenv:Body> </soapenv:Envelope>
    SOAPUI - SOAP Response
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:EmployeeManagementResponse xmlns:ns2="http://techiecommunity.net/"> <return>Test</return> </ns2:EmployeeManagementResponse> </soap:Body> </soap:Envelope>
    If interested in commenting related to JAXWS Webservice Example
    Click Here

    Name (To be shown on this Page:*

    Email (Not to be shown):*

    Website (To be shown on this Page)

    Your Comments here (To be shown on this Page):*

    (You can use <a> Tag in your comments)

    Please enter same number as shown here 186

    Name: RameshDated: 2013-12-20 10:07:39
    Thanks for this simple example on using Annotations based webservices.
    Can you show example code how to approach contract-first werbservices approach, by Creating WSDL first?    
    Name: IshtekDated: 2013-12-29 12:23:03
    Hi,
    
    I think, it is quite straight forward to follow Java implementation first to construct a webservice.
    
    I have used annotations, such as @WebService (importing javax.jws.WebService), @WenMethod (importing javax.jws.WebMethod) etc.,
     at class and method level for the implementation class file.
    
    For example, in order to expose a method called helloWorld, for the class HelloWorldService.
    This class file looks like:
    
    import javax.jws.WebService;
    import javax.jws.WebMethod;
    
    @WebService
    public class HelloWorldService {
      
       @WebMethod
       public String helloWorld() {
         return "Hello World";
       }
    }
    
    A very basic web service.
    Name: IshtekDated: 2013-12-29 12:25:53
    And some basic tutorial that I have read to start with on Java EE 6 :
    
    http://docs.oracle.com/javaee/6/tutorial/doc/bnayk.html
    Name: YogeshDated: 2013-12-29 12:31:59
    Have you read a very simple article on dzone @
    
    http://java.dzone.com/articles/jax-ws-hello-world
    Reply
    Click Here

    Name (To be shown on Page):*

    Email (Optional): (never shown)

    Website (To be shown on this Page)

    Your Comments here (To be shown on Page):*

    Please enter same number as shown here 411

    Name: IshtekDated: 2013-12-29 18:07:16
    Created a dynamic web project and a corresponding EAR file using Eclipse IDE.
    In the src folder of the dynamic web project, I have created an example service
    implementation in form of a POJO:
    
    ExampleWSImpl.java
    
    package net.techiecommunity; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class ExampleWSImpl { @WebMethod public String helloWorld() { return "Hello World"; } }
    On compiling this Java file, there will be a compiled class file placed under Project/build/classes folder. Now open up a command prompt and check for Java version, using command java -version I have following output on console:
    java version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) Client VM (build 24.45-b08, mixed mode, sharing)
    So I can use wsgen command for creating corresponding files for deploying this hello world webservice.
    wsgen -cp . net.techiecommunity.ExampleWSImpl -d ../WebContent/WEB-INF/wsdl/ -wsdl -servicename {http://techiecommunity}ExampleWS
    This above command is executed from within the folder %Project%/build/classes setting class path as '.', class such as 'net.techiecommunity.ExampleWSImpl', with the corresponding files getting generated under %Project%/WebContent/WEB-INF/wsdl/ This web service is having the service name as 'ExampleWS' and the targetnamespace as 'http://techiecommunity'. As these files are generated in the filesystem, so need to refresh eclipse project, before compiling/building this dynamic web project. If everything goes well, one can export EAR project onto the JBoss Application Server. I have used JBoss Application Server version 7.1.1, standalone mode. After JBoss application server is started successfully.. we can check certain console output to see if this webservice is deployed successfully or not.
    id=net.techiecommunity.ExampleWSImpl address=http://localhost:8080/SampleWSExample/ExampleWSImpl implementor=net.techiecommunity.ExampleWSImpl invoker=org.jboss.wsf.stack.cxf.JBossWSInvoker serviceName={http://techiecommunity.net/}ExampleWSImplService portName={http://techiecommunity.net/}ExampleWSImplPort wsdlLocation=null mtomEnabled=false
    Now you can check for the WSDL URL file using Browser:
    http://localhost:8080/SampleWSExample/ExampleWSImpl?wsdl
    Hope this helps.
    Reply
    Click Here

    Name (To be shown on Page):*

    Email (Optional): (never shown)

    Website (To be shown on this Page)

    Your Comments here (To be shown on Page):*

    Please enter same number as shown here 411




    Android Thread Example

    REST Interview Questions

    JAX-WS Webservice Example

    Android Overlap SurfaceView Example

    Spring3 Integration of File Endpoint

    JAXWS Webservice Example

    Android WebView Example

    Android Context Menu Example

    Setting up Eclipse for Android App Development

    Android SensorEvent Orientation Example

    Android ImageSwitcher Example

    REST API Design for Android App

    Android ExpandableListView Example

    Singleton Serializable on Android

    Android GPS with Google MAP Example

    Android Project With Example

    Android Project Manageing Files

    Project Ideas for App on Android Platform

    Android EditText with Background

    Android SQLite Database Example

    Android AutoCompleteTextView Example

    Android Tester

    Android Interview Questions

    Android ListView Example

    Android Intent Notification Example

    Android Intent Broadcast Example

    Android TextView Link Example

    Android Sensor List

    Android Sensor Orientation Example

    Android Intent Broadcast Receiver

    Android Gallery SurfaceViews Example

    Android Location Example

    Android Text to Speech Example

    Android Intent Example

    Android Layout Example

    Android Download File Example

    Android Expandable ListView Example

    Android Tab Example

    Android RelativeLayout Example

    Android Intent Example

    Android Text to Speech Example

    Android SQLite Example

    Android CustomView Example

    Android Canvas Example

    Android SharedPreferences Example

    Android Shape Background Example

    Android ShapeButton Example

    Android ViewFlipper Example

    Android Gallery Example

    Android TimePickerDialog Example

















       
    The content provided in this page is NOT warranted and/or NOT guaranteed by techiecommunity.net .
    techiecommunity.net is not liable for any negative consequences that may result/arise from 
    implementing directly/indirectly any information covered in these pages/articles/tutorials.
    
    For any of the content, if you would like to bring it to notice for removal from this web site, 
    please write to this web site administrator @ EMAIL-ID (please remove space before and after @),
    with appropriate concern and supporting proof(s). After thorough review and if found genuine concern,
    we would take appropriate action and remove disputed content from this web site within 24 hours 
    starting from the time it has brought to techiecommunity.net Administrator notice.
    
    While using this web site, you agree to have read and accepted techiecommunity.net terms of use and privacy policy.
       
       
    This website uses cookies to improve the user experience and gather statistics. Our advertisers use cookies too (3rd party cookies), to provide more relevant ads. Continued use of this website implies that you accept the use of cookies on this website.
    OK