Searching...
Saturday, 20 July 2013

JAX-WS Web services Example

02:01
JAX-WS stands for Java API for XML Web Services. JAX-WS is a technology for building web services and clients that communicate using XML. JAX-WS allows developers to write message-oriented as well as RPC-oriented web services.
See more about JAX-WS

Steps

1. Create a Java class to implementing Web service method
2. Create a Java class to Publish Web services
3. Start Web service
4. Check the generated WSDL
5. Use wsimport tool to generate web service artifacts needed to connect to the service.
6. Create a client class to call web services

1. Create a Java class to implement Web service

package com.pretech;

import javax.jws.WebService;

@WebService
public class StateWebService {

    public String getState(String statecode) {
        String stateName;
        if (statecode.equals("KL")) {
            stateName = "Kerala";
        } else {
            stateName = "Not avaiable";
        }

        return stateName;
    }
}

2. Create a Java class to publish Web service

package com.pretech;

import javax.xml.ws.Endpoint;

public class StateWebServicePublisher {

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/pretech/StateProvider",
                new StateWebService());
    }

}

3. Run the publisher class (StateWebServicePublisher.java)

Run the above program which we exposed the end point.

4. Check the WSDL


image 

WSDL

The Web Services Description Language is an XML-based interface definition language that is used for describing the functionality offered by a web service.
Below are the elements of a WSDL file
  • <definitions> = Root WSDL Elements
  • <types> = What data type will be transmitted?
  • <message> = What exact information is expected
  • <portType> = What operations will be supported
  • <binding> = How will the message will be transmitted on the wire ? What SOAP specific details are there
  • <service> = Define the collection of ports that make up the services and where is service located.

WSDL SOAP Binding

a WSDL SOAP binding can be either a Remote Procedure Call (RPC) style binding or a document style binding. see more about bindings here http://www.ibm.com/developerworks/library/ws-whichwsdl/
 To specify particular binding we can use the annotations over the service class as below
@WebService
@SOAPBinding(style = Style.RPC)
public class StateWebService {
   
}
Now we completed the service implementation and started the service, next we need to create the client for the service using the WSDL.

5. Generate web service artifacts needed to connect the service

1. Use command prompt and go to Java bin directory
2. Use wsimport tool to generate web service artifacts ,please see the below command 
    wsimport -d . -keep http://localhost:8080/pretech/StateProvider?wsdl
image
3. We can see the artifacts has been generated in bin\com folder
image 

6. Create a Java client to use web services

1. Create a new project and copy all the files which we generated from the previous step
2. Create a Java client to use web services
package com.pretech;
public class StateWebClient {
  public static void main(String[] args) {
        StateWebServiceService service=new StateWebServiceService();
        StateWebService ss=service.getStateWebServicePort();
        System.out.println(ss.getState("KL"));
    }
}

7.Output


Kerala
Download Source code JAX-WS-Example

0 comments: