Showing posts with label Web Services. Show all posts
Showing posts with label Web Services. Show all posts

Thursday, November 20, 2014

Create Web service client with using Axis2 in Eclipse

Create web service client with using Axis2 in eclipse

Web service helps to communicate the different application with out consuming more time even the applications are deployed in different platform. The reason behind was, the all communication is in XML. In previous tutorials briefly explain that how to create the WSDL and How to write the web service and deploy to the server and test. 

In this tutorial you able to learn that, to build the client with using the WSDL generated here

Objective : Create a web service client with using the Axis2 in eclipse.

Scope : Create we web service client with using the Axis2 and test the client service.

Requirements : 
  • Ecipse Java EE IDE
  • Axis2 - 1.6.2
  • Apache tomcat 6.0.26
Step 1 : Create a Dynamic web project  with using eclipse and name it as "ServiceClient". Right click on the "ServiceClient" project and create a web service client with using eclipse. 

The way of creating the web service client in eclipse mentioned as below. 

       New > Other > Web Service > Web Service Client


Step 2 : Chose the WSDL, which we created in the previous article. In additionally in the client generation wizard, select the Axis2 as web service run time and Tomcat 6 as a server. Click the next button to continue the wizard process.




Step 3 : Click the next button with out changing any of the parameters and finally click the finish button. Once you pressed the finish button, the auto generated code will added to the  "ServiceClient" project. (The auto generated classes are HelloworldCallbackHandler .java and HelloworldStub.java


Step 4 : In order to test the generated client, you have to write a separate class in java. In our example in order to test the client, you able to use the following "TestClient" class.

/ServiceClient/src/findanidea/webservice/services/helloworld/TestClient.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package findanidea.webservice.services.helloworld;

import java.rmi.RemoteException;

import findanidea.webservice.services.helloworld.HelloworldStub;
import findanidea.webservice.services.helloworld.HelloworldStub.SayHello;


public class TestClient {
 public static void main(String[] args) throws RemoteException {
  HelloworldStub stub = new HelloworldStub();
  SayHello sayHello = new SayHello();
  sayHello.setName("John");
  System.out.println(stub.sayHello(sayHello).getOutput());
 }
}


Once you execute the above mentioned "TestClient" class, you able to retrieve the "Hello... John" message in application console.



Hope its helps.... 

Monday, November 3, 2014

Create web service with using axis2 in eclipse


Create web service with using Axis2 in eclipse

These days web service technology is widely using in the Java EE applications. The most common reason is that, the web service is the way of communicate different applications even the applications are deployed in different plat form. For example if the currency exchange web service is build in Dot Net and deployed in one server. If another application written in Java and running in different server, that able to use the currency exchange service, (The service already developed in Dot Net) via web service client. The communication can be possible with using the XML messages over the HTTP protocol.

There are main two type of web services available. One was SOAP (Simple Object Access Protocol). The second was RestFul (Representational State Transfer). 

There are two approaches commonly used, when develop the web services.
1. top to bottom approach (WSDL to Java)
2. bottom to top approach (Java to WSDL)

The below tutorial will explain for, how to create the Soap web services based on top to bottom approach with using the Axis2.

Scope : Create a web service with using Axis2 in eclipse with using top to bottom approach.

Objective : Create an web service using Axis2 and the service operation return the Welcome message with given username.

Requirements : 
  • Eclipse Java EE IDE
  • axis2-1.6.2
  • Apache Tomcat - 6.0.26
Step 1 : Create a Dynamic web project "webservice" in eclipse. The way of creating the dynamic web project mentioned as below.
                 New > Dynamic web project

Step 2 : Copy the WSDL (you able to find here ) in to the project created in the Step 1. (/webservice/helloworld.wsdl)

Step 3 : Create the web service project as mentioned in below.
Right Click on the "webservice" project > New > Other > Web Service > Web Service

Step 4 : The following screen will appear. 



In the screen choose the "Top down Java bean Web Service" as Web service Type and choose the WSDL path as Service definition.

Select the server runtime as Tomcat 6.0 and Web service runtime as Apache Axis 2.

Step 5 : Click the next button and specify the package name for the service classes which going to generate by Axis based on the WSDL.

In this example package name specified as "findanidea.webservice.services.helloworld" .

Step 6 : Click the next button, and wait... After some time the auto generated code added to your project. 

The following java classes are generated based on the WSDL provided.




Step 7 : Do not start the server at this point and finished the wizard at this level. Now the time comes to develop the service implementation.

Step 8 : In the generated "HelloworldSkeleton" class need to incorporate the business logic.

In the below code line 3 to 5 we commented, which was auto generated when create the service. In addition to that, the business logic implemented in line number 7 and 8.

For this example, the response object contains the combination of strings Hello and name as Output property. 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public localhost.webservice.services.helloworld.SayHelloResponse sayHello(
   localhost.webservice.services.helloworld.SayHello sayHello) {
  /*// TODO : fill this with the necessary business logic
  throw new java.lang.UnsupportedOperationException("Please implement "
    + this.getClass().getName() + "#sayHello");*/
  
  SayHelloResponse response = new SayHelloResponse();
  response.setOutput("Hello... " + sayHello.getName());
  return response;
 }

Deploy the service to server.

Step 9 : Now the time to test the service service level, which we developed at Step 8.

Write the following "TestService" class under the below package. "findanidea.webservice.services.helloworld"

/webservice/src/findanidea/webservice/services/helloworld/TestService.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package findanidea.webservice.services.helloworld;

import findanidea.webservice.services.helloworld.SayHello;

public class TestService {
 public static void main(String[] args) {
  HelloworldSkeleton skeleton = new HelloworldSkeleton();
  SayHello sayHello = new SayHello();
  sayHello.setName("John");
  System.out.println(skeleton.sayHello(sayHello).getOutput());
 }
}

Step 10 : That's all folks... Now if run the "TestService" class you able to get the following response in the console.


1
Hello... John

Tuesday, October 28, 2014

How to create WSDL in Eclipse


How to create WSDL in Eclipse

WSDL (Web Service Description Language) is one of the core part of the web service development. In the WSDL file we defines what are the operations going to performs, how to access the services and service url end point etc. 

In the below tutorial you able to learn that, how to create the WSDL file for simple string manipulation function.

Objective : Create a WSDL with using eclipse.

Scope : Create a WSDL in eclipse and validate which is serve welcome message as a service response.

Requirements : 
  • Eclipse Java EE IDE

Step 1 : Create a java project in eclipse and name it as "WSDL".

Step 2 : Select the "WSDL" project and create a new WSDL file as below.
 - Right click of "WSDL" project > New > Other 
 - The New Select a wizard will appear.
 - Select Web Service > WSDL and click "Next" button on the wizard.
 - You have to name the WSDL at this point. Just name it as "helloworld.wsdl".





 - From the following wizard screen, except the Target name space rest of the settings kept as it is. Further more details of the setting able to find here.



 - Finally click the finish button.

Step 3 : Change the address to "http://localhost:8181/webservice/services/helloworld/". 

In future we are going to generate a service and client based on the WSDL created in this tutorial. 

The default operation name changed to "newOperation" to "sayHello" and save it.

The WSDL source looks like below.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:8181/webservice/services/helloworld/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="helloworld" targetNamespace="http://localhost:8181/webservice/services/helloworld/">
  <wsdl:types>
    <xsd:schema targetNamespace="http://localhost:8181/webservice/services/helloworld/">
      <xsd:element name="sayHello">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="sayHelloResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="output" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="sayHelloRequest">
    <wsdl:part element="tns:sayHello" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="sayHelloResponse">
    <wsdl:part element="tns:sayHelloResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:portType name="helloworld">
    <wsdl:operation name="sayHello">
      <wsdl:input message="tns:sayHelloRequest"/>
      <wsdl:output message="tns:sayHelloResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="helloworldSOAP" type="tns:helloworld">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="http://localhost:8181/webservice/services/helloworld/sayHello"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="helloworld">
    <wsdl:port binding="tns:helloworldSOAP" name="helloworldSOAP">
      <soap:address location="http://localhost:8181/webservice/services/helloworld/"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>


Step 4 : For validate the WSDL just right click on the WSDL source and Validate. If any errors occurs its pointed with red color.


Wednesday, October 15, 2014

How to build RESTful Web Service with Java using JAX-RS and Jersey


How to build RESTful Service with Java using JAX-RS and Jersey

Scope : Build a simple RESTful web service with using JAX-RS and Jersey.


Objective : Build an limited currency conversion RESTful service with using JAX-RS and Jersey

Requirements:
  • Eclipse JAVA EE IDE
  • JDK 1.6
  • ApacheTomcat - 6.0.26
  • The below 3 jersey related jars you have to download.
    • asm-3.3.1.jar
    • jersey-bundle-1.14.jar
    • json.jar

Step 1: Create a dynamic web project In eclipse and named as "RestCurrencyConvertService”.

File > New > Dynamic Web Project (Select the Dynamic web module version 2.5)

Add the downloaded jars to the project build path.


Step 2: Create the service "CurrencyConvertService" class under the "com.prem.restservice" package.

/RestCurrencyConvertService/src/com/prem/restservice/CurrencyConvertService.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.prem.restservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("CurrencyConvert")
public class CurrencyConvertService {

 @Produces(MediaType.TEXT_XML)
 @Path("/Convert/{from}&{to}&{amount}")
 @GET
 public String convertCurrency(@PathParam("from") String from,
   @PathParam("to") String to, @PathParam("amount") double amount) {

  // Small logic
  if (from.equalsIgnoreCase("USD") && to.equalsIgnoreCase("LKR")) {
   return "<result>" + Double.toString(amount * 130.65) + "</result>";
  } else if (from.equalsIgnoreCase("LKR") && to.equalsIgnoreCase("USD")) {
   return "<result>" + Double.toString(amount * 0.01) + "</result>";
  } else {
   return "<result>Invalid Currency Code...</result>";
  }
 }
}

Step 3: Add the servlet to the "web.xml" file.

The sample "web.xml" file as below.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>RestCurrencyConvertService</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>
 <servlet>
  <servlet-name>CurrecnyConvert</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <init-param>
   <param-name>com.sun.jersey.config.property.packages</param-name>
   <param-value>com.prem.restservice</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>CurrecnyConvert</servlet-name>
  <url-pattern>/Convert/*</url-pattern>
 </servlet-mapping>
</web-app>

That's all folks... 

Step 4: Now the time to execute and see the result.... 

Right click on the "RestCurrencyConvertService" project and go to Run As - Run on Server.

You able to see the below screen when you paste the below URL to browser and enter.
http://localhost:8181/RestCurrencyConvertService/Convert/CurrencyConvert/Convert/USD&LKR&100





http://localhost:8181 - The "ApacheTomcat - 6.0.26" server runs on the 8181 port at my local                                             machine
/RestCurrencyConvertService - Display name in the "web.xml" file
/Convert - The url pattern defined in the "web.xml"
/CurrencyConvert - @Path("xxxxxxxx") defined in the class level
/Convert - @Path("xxxxxxxx/{from}&{to}&{amount}") defined in the method level.
/USD&LKR&100 - @Path("/Convert/{xxx}&{yyy}&{zzz}") defined in the method level.

 In this example xxx = USD, yyy = LKR and zzz = 100. 

Hope its clear...