RSS

Tag Archives: axis2

Java SOAP Web Service With Apache Axis2 in Netbeans

<

Requirements

  1. Java Development Kit (JDK)
  2. NetBeans IDE
  3. Apache Axis 2
  4. Apache Tomcat Server

Main Topics

  1. Setup Development Environment
  2. Create Web Service
  3. Deploy Web Service to Tomcat Server

1. Setup Development Environment

Install Axis2 Support Plugin

Go to Tools >> Plugins and open Plugin Manager. Check whether Axis2 Support Plugin is already installed. Otherwise you may need to find it from Available Plugins and install.

Configure Axis2 for Tomcat

Extract axis2.war archive to Tomcat webapps directory.

You can find this directory in your CATALINA_BASE/webapps. If you are not sure where CATALINA_BASE points to, open Services tab and select Tomcat server. Right click Tomcat server and select properties. Most probably you will find it there. If there is no CATALINA_BASE, you have not run your Tomcat before. Start and stop Tomcat to allow this varible generate.

Start or restart the Tomcat server. Tomcat unpacks the axis2.war file into an axis2 folder in CATALINA_BASE/webapps.

Go to Tools >> Options and click Axis2 icon to view deployment options. Set the target location for Axis2 AAR files to your CATALINA_BASE (not TOMCAT_HOME)/ webapps/axis2 directory.

Make sure the Axis2 URL field contains the correct port number for your Tomcat server. To check the port number, start Tomcat (from the Services tab or from Tools -> Servers) and see what port Coyote HTTP/1.1 uses.

select Use Tomcat Manager for Deployment

Check whether username and password of manager role are correct by referring CATALINA_BASE/conf/tomcat-users.xml.

2. Create Web Service

File >> New Project. The New Project wizard opens. From the Java category, select a Java class library project. Click Next.

Right-click the project and choose New >> Other. The New File wizard opens. From the Web Services category, choose Axis2 Service from Java and click Next.

Keep settings as it is in the following wizard and click Next.


In this wizard enter Java Web Service class name and click Finish.


The IDE generates a HelloAxisWorld.java class in the axishello source package and a HelloAxisWorld Axis2 web service that mirrors this Java class.

3. Deploy Web Service to Tomcat Server

Right-click the web service’s node. The context menu opens. Select Deploy to Server. The IDE compiles an Axis2 AAR file and copies it to the axis2.war file used by the application server.

If you enabled automatic deployment in Tomcat Manager, axis.war will automatically redeploy to server whenever it ‘s get updated.

To test the service, expand the web service node to reveal the operations. Right-click the hello:String node and select Test Operation in Browser.

To test the service, expand the web service node to reveal the operations. Right-click the hello:String node and select Test Operation in Browser.

Browser opens with a test value of your variables. You can find test value in the URL. Change it and refresh the page.

 

 
9 Comments

Posted by on November 3, 2012 in J2EE, Java

 

Tags: , , ,

Java SOAP Web Service With Apache Axis2 in Eclipse

Web Service

Today we are going to build a SOAP based Java web service. We are using AXIS2 which serves as a SOAP implementation. I assume that you have already installed Java JDK in to your machine.

Requirements :

1) Eclipse EE – Download
2) Apache Tomcat – Download
3) Apache Axis2 Binary Distribution – Download
4) Apache Axis2 WAR Distribution – Download

While installing these programs, consider about default ports. There may be port conflicts if you neglect this fact. This becomes an issue only if you are allocating ports which are already used by previously installed programs. For an instance, there may be a port conflict when attempting to install Tomcat server using port 8080 in a machine where Oracle 10g is installed. Both of these use port 8080 by default. So you may change the ports as necessary during or after the installation.

Please note that there may be various compatibility issues when using Eclipse and Apache Tomcat with Axis2 to build our web service. Most of the problems can be minimized with Netbeans. GlassFish server supports out of the box in many occasions with Netbeans.
Let’s deal with Netbeans at a later time.

I’m using Eclipse Indigo, Apache Tomcat 6 and Axis2 for this tutorial.

Extract Apache Axis2 Binary Distribution. Create an Environment Variable as AXIS2_HOME. Get the path to Axis2 installation directory and use it as the value of AXIS2_HOME Environment Variable. (something like C:\Program Files\axis2-1.6.2)

Extract Axis2 WAR Distribution and copy axis2.war file to Tomcat server webapps directory.

Now you are ready to go ahead with this tutorial. First check our road map.
1. Setup Development Environment in Eclipse.
2. Create Web Service.
3. Deploy web service.
4. Create a client who consumes Web Service.

1. Setup Development Environment in Eclipse.

Now you need to configure Eclipse for Tomcat server and Axis2.
Open Eclipse IDE and go to Window >> Preferences
Add new server runtime environment.

Select your Tomcat server version and click Next. Browse your Tomcat installation directory in next window.

Now configure Axis2 in Axis2 preferences under Web Services category. Browse your Axis2 installation directory. You should see this message if this is fine.
“Axis2 runtime loaded successfully”

Select Server and Runtime. Make sure your server runtime is selected. Web service runtime should be Apache Axis2.

 

2. Create Web Service.

First create a new Dynamic Web Project (File >> New >> Other) and choose Web >> Dynamic Web Project. Enter project settings in the first window.
Select the target runtime that we configured earlier.
You should modify the configuration so that Axis2 is included into the project.

Remeber to put a tick on Axis2 web services

Click Next>>Next and Finish. Now Eclipse will load the project.
Now it’s time to create web service class. This class can contain some methods which are exposed to outside. The clients are going to consume this web service class. First we create a Plain Old Java Class and convert it into a web service. Right click on project and select New>> Class

Add a public method to this class.

package com.codezone4;

public class MyService
{
 public String testMethod(String param)
 {
 return "Read " + param;
 }
}

To expose this file as a web service right click on the Java file and select Web Services >> Create Web Service
or
select File >> New >> Other and choose Web Service.
You can can add web service using one of above methods.

We use bottom-up approach as web service type. Select the class which acts as the web service(that we just created). Make sure your configurations are correct.

Click Next. Make sure Generate a default service.xml file is selected and click Next. Now you are asked to Start Server. Do it!
Then Click Next and Finish the dialog.
Now you should be able to reach your web service through your browser. Tomcat server is started and you gonna run your web service in Tomcat.
Right click on your project and select Run as >> Run on server.

You may see the below screen through your Eclipse web browser. Alternatively you can copy the following URL into your web browser bar.
http://localhost:8440/HelloWorldWebService/
This port number 8888 depends on your Tomcat configuarations.

Go to Services. Under Available Services you will find your own service. Click on MyService link (You will find your web service class name here).
Can you see bunch of beautiful xml? This is your WSDL(Web Service Description Language). The clients who consume your service communicate with this WSDL to access the functionality of the web service.
Copy the URL for WSDL file.
You can validate this file at a later time when you create the client. To do that enable WSDL validation for all files in Eclipse preferences. This option can be found under web service category.

Obviously you can play with your web service without a real client using Web Service Explorer tool in Eclipse. Click that icon

This will launch a screen similar to the following.

Click WSDL page icon. On the next screen, click WSDL Main under navigation section.
Enter your WSDL URL into the box and click Go. On the left you will see a hierarchical representation of your web service. Play around with this for a while and test your web service with different inputs….

3. Deploy web service.

In current situation, the web service can only be run within Eclipse. So we are going to deploy it to Tomcat server. First create .aar (Axis Archive)
Got to your web service directory using command prompt.
Eg. C:\Users\AAA\Documents\Eclipse\HelloWorldWebService\WebContent\WEB-INF\services\MyService

Enter following command to create .aar file.
jar cvf HelloWorldWebService.aar com META-INF
This will generate .aar file in your web service directory. Now wanna upload this file using the Administration panel in Axis2. You can enter this area using Axis home page Administration link.
username : admin
password : axis2

Or else you can simply copy the .aar file and paste into Tomcat webapps\axis2\WEB-INF\services directory.

To verify your deployment first stop Tomcat server in Eclipse. Click on red, small rectangular button on Servers panel to stop Tomcat.
Next go to Tomcat bin directory and find startup.bat file. Run this file to start Tomcat. If you are using Mac run startup.sh file.
Then go to your web service URL.
http://localhost:8440/HelloWorldWebService/

 

4. Create a Web service client

Select File >> New >> Other and choose Web Service Client.
Enter WSDL URL into the box. Here you can browse for WSDL and validate if needed.

This will generate two new classes called MyServiceStub.java and MyServiceCallbackHandler.java. Now we can create client. Create new class called TestClient.java and paste following code.

package com.codezone4;

import java.rmi.RemoteException;
import com.codezone4.MyServiceStub.TestMethod;
import com.codezone4.MyServiceStub.TestMethodResponse;

public class TestClient {

 public static void main(String[] args) throws RemoteException {
 MyServiceStub stub = new MyServiceStub();
 TestMethod method = new TestMethod();
 method.setParam(" Cheers!");
 TestMethodResponse response = stub.testMethod(method);
 System.out.println(response.get_return());

}

}

Run this client as a Java application. Observe the output in console. You should see the message that resulted from remote method call if everything worked fine.

I think you enjoy this web service tut. Please leave a comment.
Happy coding!

 
2 Comments

Posted by on November 3, 2012 in J2EE, Java

 

Tags: , ,