Creating and testing a web service using Soap UI
Tech-Today

Creating and testing a web service using Soap UI


I just want to share a simple example on how to create and test a web service on java.

Download and install the following:
1.) eclipse java (client)
2.) eclipse jee (web service)
3.) jboss 5.1 AS
4.) soap ui, get the stand alone (http://www.soapui.org/)

For this example we will create a simple Calculator class.

Creating the web service:
1.) Open your eclipse-jee
2.) Create a new "Dynamic Web Project" and name it "WebServiceDemo", by clicking File->New->Dynamic Web Project
3.) Create a new class Calculator, see the ff class:
package com.kalidad.webservices;


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

@WebService
public class Calculator {
@WebMethod
public double add(double a, double b) {
return a + b;
}

@WebMethod
public double multiply(double a, double b) {
return a + b;
}
}
The @WebService tag, tells us that this class is a web service :-)
4.) Add the ff in your web.xml file located in WebContent/WEB-INF


Calculator
com.kalidad.webservices.Calculator


Calculator
/calc

It tells us that we can access the web service by typing /calc in the url.
5.) I assume you already setup jboss server, open the server view and Add Remove the newly created project to the Jboss 5.1
6.) Publish and start the server.
7.) Open the ff URL: http://localhost:8080/WebServiceDemo/calc?wsdl, you should see a page like:

Before we would create a java client to call the web service we would validate first if it's really working properly by using SoapUI.
1.) Run SoapUI.bat
2.) Create new SoapUI project.
3.) In the initial WSDL input type: http://localhost:8080/WebServiceDemo/calc?wsdl, click ok.
The sample Test Requests:
To test the sample requests, let's add some sample response
4.) Right click calc?wsdl and select New Test Suite.
5.) Right click test suite and select Add Test Case.
6.) Right click TestCase1, select Add Step->Mock Response
7.) Operation=add, Interface=CalculatorBinding, Port=8088, Path=/calc, checked Create Response
8.) Double click TestCase1, a panel will open in the right side and click the green triangle button.
9.) Now to run the request, Double click Request 1 and again click the green triangle button, you should see the reply now. Note that you can point the request to the mock service that we have or from the webservice that we created by changing the url in the top panel.
10.) If you want to change the value in the mock response, just double click "Mock Response" and change the return value.
Sample request:
2+2

2*2


Now we want to test our webservice by creating a java client.
1.) Generate the java classes, by invoking:
C:/jboss/jboss-5.1.0.GA/bin/wsconsume -o c:\test\calc -k http://localhost:8080/WebServiceDemo/calc?wsdl, it should create java and .class files in c:\test\calc directory.
2.) Run eclipse-java, and create a new java project, WebServiceDemoClient.
3.) You will notice, that there are compilation errors, just comment the code where it came from.
4.) Create a new class Client.
package com.kalidad.webservices;


public class Client {
public static void main(String args[]) {
new Client();
}

public Client() {
CalculatorService service = new CalculatorService();
Calculator calc = service.getCalculatorPort();
double result = calc.add(2d, 4d);
System.out.println("2 + 4 = " + result);

result = calc.multiply(2d, 4d);
System.out.println("2 * 4 = " + result);
}
}
5.) Export the project as jar file by File->Export->Java->jar file. Just click next->next...->finish. Note I save the client.jar in c:\test folder.

6.) Now the setup is complete, we just have to run the client using wsrunclient. from jboss.
C:/jboss/jboss-5.1.0.GA/bin/wsrunclient -classpath "C:/jboss/jboss-5.1.0.GA/client/client/jbossall-client.jar;c:/test/client.jar" com.kalidad.webservices.Client
It should output:
And we're done :-).
Take note of the jboss directory, I didn't set it on the environment setting because I have several versions installed for different project so the path is hardcoded.




- Javaee Development
JavaEE6How to create a javaee6 web app using jboss maven war archetypeCreate a simple javaee6 web app with maven, glassfish and postgresqlHow to validate a JavaEE6 Bean in a jobHow to add JavaEE 6 archetypes in eclipse keplerHow to create a custom bean...

- How To Use Testng With Eclipse
This tutorial will teach you how to setup and run TestNG on eclipse. What you need: 1.) Download and setup eclipse-java-helios 2.) Download and extract TestNG (http://testng.org/doc/download.html) in c:\java\testng-version 2.) Install TestNG on eclipse...

- Creating Your First Ejb3 Project In Eclipse On Jboss Server
This tutorial will teach you on how to setup and run an ejb application. What you need (noted are where I installed my versions): 1.) Jboss 5 (jboss-5.1.0.GA) 2.) eclipse-jee 3.) ojdbc14 (C:\jboss-5.1.0.GA\server\default\lib) Steps: 1.) Create a new EJB...

- Create A New Seam Web Project In Eclipse-helios
In this exercise we will be building seam web project in eclipse. It's long so stay focus :-D. What you need (The following should be installed correctly): Note: in parentheses is where I installed mine. 1.) Jboss seam 2.2.1 (C:\jboss-seam-2.2.1)...

- Setting Up Eclipse, Subclipse And Visual Svn Server
If you are tired of the usual copy all, zip backup your application files then I suggest you try using SVN. But let me clarrify, SVN is not a backup management tool. Instead it's a way of properly tagging each file, fix/update wherein you can put...



Tech-Today








.