Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses
Showing posts with label Web Services. Show all posts
Showing posts with label Web Services. Show all posts

Part 1 - How to create Jersey JAX-RS Restful Web Services ? (Video Tutorial)



Video tutorial to demonstrate how to create Jersey JAX-RS Restful Web Services.






How to create, deploy and test JAX-WS SOAP based Web Services in NetBeans ? (Video Tutorial)



Video tutorial to demonstrate how to create, deploy and test JAX-WS SOAP based Web Services in NetBeans.






SOAP Web Service in Java (JAX-WS)

In this tutorial, we will show you how to develop a simple SOAP based Web Service in Java using JAX-WS, called as "CalculatorService" in NetBeans 7.3. In order to demonstrate development of this application we begin with:

1. Create a Web Application named as "Calculator" in NetBeans.
2. Creating a SOAP Web Service called as "CalculatorService"
3. Creating a simple operation called as "sum".
4. Deploy and Test the Web Service.

Create a Web Application named as "Calculator" in NetBeans.

Step 1 :- Open NetBeans IDE (See fig below)



and Select in the menu bar File ---> New Project or press Ctrl + Shift + N. (See fig below)


New Project dialog box gets open.

Step 2 :- Under Categories: select Java Web.
Step 3 :- Under Projects: select Web Application.
Step 4 :- Click Next > . (See fig below)































New Web Application dialog box gets open.

Step 5 :- Under Name and Location tab, enter Project Name: as "Calculator".
Step 6 :- Click Next > . (See fig below)



Under the same New Web Application, Server and Settings dialog box gets open.

Step 7:- Choose Server: as "GlassFish Server 4.0". You can also choose "GlassFish v3 Domain" as server if you are using old version of NetBeans other than 7.3.1, which comes bundled with "GlassFish v3 Domain".

Step 8:- Choose Java EE 6 Web as Java EE Version:. Keep rest as default. 
Step 9:- Click Finish. (See fig below)



Creating a SOAP Web Service called as "CalculatorService"

Step 1:- Right click on Calculator project and Select New ---> Web Service... (see fig below) 



Step 2:- New Web Service dialog box gets open. In the Web Service Name: textfield enter name as "CalculatorService".

Step 3:- Enter the package name for the CalculatorService Web Service.

Step 4:- Click Finish. (see fig below)




Creating a simple operation called as "sum".

Step 5:- After creating Web Service by name "CalculatorService". Under Web Services directory of the project, right click on the CalculatorService created and click on "Add Operation". (see fig below)
































Add Operation dialog box gets open.

Step 6:- Enter name of the operation to expose as Web Service method. Here provide as "sum".

Step 7:- Enter the return type for the method. Here sum method will calculate sum of two numbers "number1" and "number2" and return it as a int value.

Step 8, 9 and 10:- Enter two parameters by clicking add button as "number1" and "number2" of type int, whose sum is to be calculated in the method sum. Click OK. (see fig below)




















































Open Web Service class by name "CalculatorService". Operation by name sum gets created having return type as int. It gets in two parameters as number1 and number2 of the type int. The java class is now a Web Service as it is annotated by @javax.jws.WebService. The operation sum becomes the exposed method of the Web Service as it is annotated by @javax.jws.WebMethod. This method takes in two SOAP request parameters of type int annotated as @javax.jws.WebParam. (see java class below)

package com.hubberspot.webservice;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

// @WebService annotation makes a class a Web Service
@WebService(serviceName = "CalculatorService")
public class CalculatorService {

    // @WebMethod annotation expose a method as a service.
    @WebMethod(operationName = "sum")
    // @WebParam annotation indicates parameters to method coming from SOAP request.
    public int sum(@WebParam(name = "number1") int number1, 
                   @WebParam(name = "number2") int number2) {
        
        int sum = 0;
        
        sum = number1 + number2;
        
        return sum;
    }    
}



Deploy and Test the Web Service. 

Step 1:- Right click on the "Calculator" project directory and click "Deploy". The Web Service gets deployed on the GlassFish Server.(see fig below)




























Step 2:- Under Web Services directory of the project, right click on the CalculatorService created and click on "Test Web Service". It opens a browser window to test the CalculatorService Web Service (see fig below)

































A browser window gets open and GlassFish Server creates a Tester client on the URL "http://localhost:8080/Calculator/CalculatorService?Tester". The Tester client has a link to the WSDL file created for the Web Service. The link to WSDL file is at : "http://localhost:8080/Calculator/CalculatorService?WSDL" (see fig and WSDL file below).

Step 3:- In order to test sum method exposed as Web Service. Enter number1 parameter value on the Tester client say 5.

Step 4:- Enter number2 parameter value on the Tester client say 10.

Step 5:- After the entering the value for number1 and number2 variables click sum button.

WSDL file for the CalculatorService Web Service -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<definitions targetNamespace="http://webservice.hubberspot.com/" 
             name="CalculatorService" xmlns="http://schemas.xmlsoap.org/wsdl/" 
             xmlns:wsp="http://www.w3.org/ns/ws-policy" 
             xmlns:tns="http://webservice.hubberspot.com/" 
             xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
             xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" 
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
             xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" 
             xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://webservice.hubberspot.com/" 
                  schemaLocation="CalculatorService_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="sum">
    <part name="parameters" element="tns:sum"/>
  </message>
  <message name="sumResponse">
    <part name="parameters" element="tns:sumResponse"/>
  </message>
  <portType name="CalculatorService">
    <operation name="sum">
      <input wsam:Action="http://webservice.hubberspot.com/CalculatorService/sumRequest" 
             message="tns:sum"/>
      <output wsam:Action="http://webservice.hubberspot.com/CalculatorService/sumResponse" 
              message="tns:sumResponse"/>
    </operation>
  </portType>
  <binding name="CalculatorServicePortBinding" type="tns:CalculatorService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="sum">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="CalculatorService">
    <port name="CalculatorServicePort" binding="tns:CalculatorServicePortBinding">
      <soap:address location="http://localhost:8080/Calculator/CalculatorService"/>
    </port>
  </service>
</definitions>


On clicking sum button, "CalculatorService" Web Service method called as sum gets executed taking in 5 and 10 as int parameters and returns sum as 15.

Step 6:- It shows method parameters as 5 and 10 of type int.
Step 7:- The return value after execution of sum method.
Step 8:- It shows the SOAP request for the "CalculatorService" Web Service which is been send in the form of xml. It is sending the values for number1 and number2 for the sum method.
Step 9:- It shows the SOAP response coming from the "CalculatorService" Web Service, returning the output of sum of number1 and number2.(see fig below)




How to create a Java client for consuming SOAP Web Service ?.

After deploying CalculatorService Web Service in GlassFish Server, refer post : How to create your first SOAP based Web Service in Java using JAX-WS ? , the next step is to make a Java client that can consume it. In above article, CalculatorService had a method as sum. In this post we will create a Java client for consuming sum by passing in two numbers and getting back the sum of it. The steps involved in creating Java client are as follows :

1. Creating a Java application as CalculatorServiceClient.
2. Adding Web Service client to the Java application.
3. Creating a Java client class for consuming CalculatorService Web Service.
4. Build and Run the Java client.


Creating a Java application as CalculatorServiceClient.


Step 1 :- Open NetBeans IDE (See fig below)



and Select in the menu bar File ---> New Project or press Ctrl + Shift + N. (See fig below)


New Project dialog box gets open.

Step 2 :- Under Categories: select Java.
Step 3 :- Under Projects: select Java Application.
Step 4 :- Click Next > . (See fig below)



Step 5 :- Under Name and Location, provide Project Name: as "CalculatorServiceClient".
Step 6 :- Select check box for (Create Main Class) as it creates a main class which will be our Web Service Java Client.
Step 7 :- Click Finish. (see fig below)



Adding Web Service client to the Java application.

Step 1:- Right click CalculatorServiceClient project directory and browse to New ---> Other. (see fig below)




Step 2:- New File dialog box gets open. Under Categories: select Web Services.
Step 3:- Under File Types: select Web Service Client
Step 4:- Click Next >. (see fig below)



Step 5:- New Web Service Client dialog box gets open. Under WSDL and Client Location , specify the WSDL file location for the CalculatorService Web Service by following options as Project: , Local File: , WSDL URL: , IDE Registered:. Select WSDL URL and provide the url for the deployed CalculatorService WSDL on the localhost. As we have deployed CalculatorService on the GlassFish Server in the above mentioned post, here it is pointing at "http://localhost:8080/Calculator/CalculatorService?WSDL", localhost is the ip address for it. If Web Service is deployed on another server provide the ip address for it on the WSDL url.

Step 6:- Enter the package name.
Step 7:- Click Finish. (see fig below)



Step 8:- It creates client side artifacts for the Service Endpoint Interface (SEI), which acts as a Proxy for the SOAP Web Service. (see fig below)




Creating a Java client class for consuming CalculatorService Web Service.

Open the CalculatorServiceClient.java and add the following code to it.

package calculatorserviceclient;

import com.hubberspot.webservice.client.CalculatorService;
import com.hubberspot.webservice.client.CalculatorService_Service;

public class CalculatorServiceClient {

    public static void main(String[] args) {

        int number1 = 6;
        int number2 = 12;

        // Create the proxy Web Service and call the sum method
        // using the port of the proxy service.
        CalculatorService_Service service = new CalculatorService_Service();
        CalculatorService port = service.getCalculatorServicePort();

        // retrieve the sum of number1 and number2
        int sum = port.sum(number1, number2);

        System.out.println("The Sum of two numbers : " + number1
                + " and " + number2 + " is " + sum);
    }
}



Build and Run the Java client

Step 1:- Right click CalculatorServiceClient project directory and select Build.
Step 2:- Run the CalculatorServiceClient.java class

  

Output of the program : 

 
 
© 2021 Learn Java by Examples Template by Hubberspot