Friday, July 8, 2011

Create a Client for a JAX-WS Web Service

JAX-WS (Java API for XML Web Services) technology can be use to develop both web services & clients which communicates via XML. So all the calls and responses are transmitted as SOAP messages (XML files). Here I'm going to develop my servicewith netbeans IDE.

Creating a Web Service

  1. Select file -> new project from the menu and choose the project category as java web -> web application.
  2. Name the project as "WSApplication" then select the server appropriately. Here i used GlassFish server. 
  3. After creating the project we are going to create the web service. First right click on the project and select New -> Web Service. Then name the service and select a location or the package. According to the my sample, name the service as MultiplicationService and the package name as jaxws.
  4. Now we are going to implement our service. The class MultiplicationService is annotated with @WebService annotation as the web service endpoint and it declares a simple method multiplication, with @WebMethod annotation (to exposed the the method to the client). Finally the method returns the multiplication of given two parameters.
  5. package jaxws.multiplication;
    import javax.jws.WebService;
    import javax.jws.WebMethod;
    public class MultiplicationService {
        public int multiplication(int i, int j) {
            return i * j;
  6. Now you can deploy your service and run it. Right click on your project and select deploy, then you can run your application.
  7. Using wsimport command we can generate all the JAX-WS artifacts by importing the,  
  8. http://localhost:8080/WSApplication/MultiplicationServiceService?WSDL  
  9. Open the command prompt and import artifacts in to a new package. First move to the java directory in the netbeans project. 
  10.  > cd WSApplication\src\java
     > wsimport -p soap -keep http://localhost:8080/WSApplication/MultiplicationServiceService?WSDL
      -p       -> Specifying a target package           
      -keep  -> Keep generated files                     
    So the above command generates the java artifacts and compile them by importing the http://localhost:8080/WSApplication/MultiplicationServiceService?WSDL
  11. So this is the new view of the project window

Creating the Client

After importing JAX-WS artifacts, it's an easy task to create a client for the multiplication service by using MultiplicationService and MultiplicationServiceService classes (generated by the wsimport command). Code for the sample client is given below. It is giving the result of the multiplication method.
package jaxwsclient;

import soap.MultiplicationServiceService;
import soap.MultiplicationService;

public class Client {

    public static void main(String[] args) {

        MultiplicationServiceService service = new MultiplicationServiceService();
        MultiplicationService port = service.getMultiplicationServicePort();

        System.out.println(port.multiplication(5, 2));

So it's all about creating a simple client for a jaxws web service. Hope this will be useful for u all....

No comments:

Post a Comment