Saturday, December 8, 2012

Create java web service in Eclipse using Axis2 (Part 03)

This is the third part of create java web service using Axis2 tutorial. In this post I'm going to create a simple client program for the web service developed in previous tutorial. Before creating this program you have to complete my previous tutorials.

1. Create java web service in Eclipse using Axis2 (Part 01)
2. Create java web service in Eclipse using Axis2 (Part 02)

Before implementing the client first we have to find the wsdl file.
First run the created project on Tomcat server. To do this right click on project select Run As then select Run on Server.


Click on Services

Select Hello which is your web service class name. Under Available Operations you can see your web service operation.

 After clicking on Hello you can see the  WSDL file. Copy WSDL link to the clip board.



OK. We coppied the WSDL link.

Now we have to create the web service client.
Right click the web service class select New >> Other >> Web Services >> Web service Client. 



Click Next. Paste link of the wsdl file to the Service definition box. (If you want to access the wsdl in another computer via a net work replace "localhost" in the WSDL url with the ip address of the server machine)
Make sure that the Server runtime is Tomcat & the Web service runtime is Axis2 by clicking the links underlined red color in the following diagram.

Client type : Java Proxy




 


Click finish to generate Callback Handler & the Stub in same package where the web service class exists.(If you want to change the client package click next & give a package name to Custom package name as you wish)

Then create new class. Class name that I used is Client. Here is the project structure.



Following code shows how we can invoke the web service method using auto generated Stub. Paste this code to newly created class. This client class also in the same package with  web service class. 

 
package org.webapp.ws;
import org.webapp.ws.HelloStub.SayHello;

public class Client {
 public static void main(String[] args) throws Exception {
     HelloStub stub = new HelloStub();
    
     //Creating the request
     org.webapp.ws.HelloStub.SayHello  request = new  org.webapp.ws.HelloStub.SayHello();
     request.setName("Java web");
   
     //Invoking the service
     org.webapp.ws.HelloStub.SayHelloResponse response  = stub.sayHello(request);
     System.out.println("Response : " + response.get_return());
    }
}


Right click Client class and run the client as a java application.



Result :


Click here to download the complete project with client.

If you find this post helpful don't forget to leave a comment. Your comments always encourage me to write more!

Happy coding!

Create java web service in Eclipse using Axis2 (Part 02)

This is the second part of create java web service using Axis2 tutorial. In this post I'm going to demonstrate the configuration of Apache Axis2 in Eclipse and create a simple java web service using Apache Axis2 SOAP engine. (If you want to see the first part click here.)

1. First you need to download and extract Apache Axis2 SOAP engine.

 Apache Axis2 Binary Distribution - DOWNLOAD

2. Configure Axis2

In Eclipse go to Window >> Preferences >> Web Services >> Axis2 Preferences




 Then Browse to axis2 extracted location. If you configure correctly Eclipse will show this message "Axis2 runtime loaded successfully" then click on Apply button.


To set server & runtime in the same Preferences window click on Server & Runtime.



 Then select Tomcat 7 as Server runtime & Axis2 as Web service runtime.



To complete configurations finally click Apply and OK.

3. Creating the Dynamic web project
After completing those configurations we can directly jump in to the web service development part. Here I'm going to implement very simple project. You can change it as you want.

In Eclipse EE create new Dynamic web project. Give project name



- You have to select Tomcat 7 as Target runtime.
- Select 2.5 in Dynamic web module version drop down menu.
- Then click Modify... button appears just after the Configuration.
- I gave the "WebApp" as project name.


- Select Axis2 web services in opening window.


Click Finish to create the Dynamic web project.

Right click on src folder then New >> Class


- Give class name as well as package name
- I gave package name as "org.webapp.ws" and class name as "Hello"


- Then click finish to crate the class.

Add following code to the class


public class Hello {
 public String sayHello(String name) {
    return "Hello !!! " + name;
 }
}

4. Creating the web service

Right click on your service class (not the project).
Select Web Services >> Create Web Service



In Web Service window tick to the Monitor the Web service check box. Other default settings are OK. No need to change those to this web service.


Click Next.
Next window, select Generate a default services.xml file.



Then click Next and finally click Finish.

4. Testing the project
Perfect...... Now you should have a web service. To set the service Right click on your project and select Run As >> Run on Server.


Click next & click Finish.

If everything went fine you should see following page in Eclipse internal web browser.


Click here to download the project.
In my next post I hope to demonstrate the implementation of the client program.

If you find this post helpful don't forget to leave a comment. Your comments always encourage me to write more!

Thank you for watching.
Happy coding!

Create java web service in Eclipse using Axis2 (Part 01)

This series of tutorials will explain how to create a simple java web service in Eclipse using Axis2. Finally I hope to create a client program for the web service.
This is the part 01.
Here I'm going to explain how we can configure Tomcat 7 for Eclipse 4.2 (Juno) because before create a java web service we need to setup development environment and configure the server. Here I use Tomcat 7 server for this tutorial.

1. Setup the development environment.
To setup the development environment you need to download and extract Eclipse EE and Tomcat servlet container.
1. Eclipse IDE for Java EE Developers - Download
2. Apache  Tomcat (Download zip archive not the windows service installer) - Download.

2. Here is the steps to configure Tomcat 7 in eclipse
2.1 Double click to open Eclipse. You should be able to see the Servers tab.


If you can't see the Servers tab then ;

 Window >> Show View and select Servers


2.2 Right click on Servers tab
Select ; New >> Server


 

Then from the new server window select ; Apache >> Tomcat v7.0 Server and click next 



In next window browse to extracted Tomcat files, that is Tomcat installation directory and select the jre that installed in your system and click finish to create the new server.



2.3 To start the server right click on Tomcatv7 server in Servers tab and select Start.



2.4 To check the server open Chrome and go to localhost:8080 you should be able to see Tomcat start page.


If you are getting "requested resource is not available" error you need to change the file location of the Tomcat.

 
In order to do that right click on server in the Servers tab and select properties. Then from the server properties window click Switch Location and Apply , OK.



Next double click on the server to open Overview.



 In the Overview window  change Server locations to Use Tomcat installation and save changes. (If you want you can change ports from the Overview window).


Restart the server and check. Now you should be able to see the default start page.



To open Eclipse Internal web browser select ;
Window >> Show View >> Other >> General >> Internal web Browser 


 

PART 02 (Configuration of Axis2 and creating simple web service)

Thanks!
Post your ideas.