Friday 6 December 2019

Create and Consume Web Service | SOAP

SOAP WEB SERVICE

This blog describes below two steps in details using Eclipse
1) Create Web Service
2) Consume Web Service
..................................................................................................................................................................

1) Create Web Service

  • Create New Java Project in Eclipse
  • Create new package and create one class in it called Forms.java
        package com.praveenws.forms;
      import javax.jws.WebService;
      @WebService
      public class Forms {     
        private String firstName;
        private String lastName

        public void addFirstName(String fname) {
             firstName = fname;
        }
        public void addLastName(String lname) {
             lastName = lname;
        }
        public String fetchUserName() {
             return (firstName + lastName);
        }
      }


  • @WebService annotation tells Java interpreter that we intend to publish ALL the methods of this class as a web service. If we want to publish only particular methods then we can use @WebMethod annotation before the method signature. In order to publish our class and its methods as web service we need to crate appropriate stub files or artifacts for web service deployment and invocation. Fortunately Java provides a tool called ‘wsgen’ which generates JAX-WS portable artifacts used in JAX-WS web services.
  • Open command prompt in project path and hit below command
                                wsgen -cp bin -d bin com.praveenws.forms.Forms


            Note
                    cp -> classpath for our class ie bin
                    d  -> tell where to place generated output files which is also bin

  • Create one more package with class say FormsEndpointPublisher,java 
         package com.praveenws.forms.endpoint;
      import javax.xml.ws.Endpoint;
      import com.praveenws.forms.Forms;

      public class FormsEndpointPublisher {
          public static void main(String[] args) {
                Endpoint.publish("http://localhost:8881/FormWS/Forms"new Forms());
          }
      }
     
     NoteEndpoint.publish method publish the class as a web service endpoint.
  • Run this class as ‘Java Application’.
You will not get output in the Console. To check whether our class is published as web service, open a browser and type the URL mentioned in the endpoint with a parameter ?wsdl appended.
http://localhost:8881/FormWS/Forms?wsdl

  • When you run the application, the Java SE platform has a small web application server that will publish the web service at the address while the JVM is running. If you see a large amount of XML that describes the functionality behind the web service, then the deployment is successful.

2) Consume Web Service

  • Create New Java project which will be client for using our web service.
  • Just like ‘wsgen’, JAX-WS also provides a tool called ‘wsimport’ for generating the artifacts required for creating and consuming a web service. ‘wsimport’ takes a wsdl file as input.


  • From the project folder in command prompt or terminal, issue the following command,
       wsimport -s src -d bin http://localhost:8881/FormWS/Forms?wsdl

  • Now refresh the current project, you will notice it generated lot of files here to support exising web service which can be used now in this project.
  • Now create new pakage with a Java class say FormsClient.java to run this client
          package com.praveenws.forms.client;
     import com.praveenws.forms.Forms;
     import com.praveenws.forms.FormsService;

     public class FormsClient {
       public static void main(String[] args) {
            
             FormsService formService = new FormsService();
             Forms forms = formService.getFormsPort();

             forms.addFirstName("Chacha");
             forms.addLastName("Choudhary");
            
             System.out.println("Name : " + forms.fetchUserName());
        }
      }
  • Run this class as Java Application, you will get the following output in the console.
           Name : ChachaChoudhary

      Note: FormsService class got created automatically in project when imported web service.

No comments:

Post a Comment