This entry
shows how to create a simple Web service based on JAX-WS. The eclipse project
is a Maven project and therefore the following elements are necessary to
implement this:
1) Maven
3.0.4.
2) JUnit
4.0
3) JDK
1.5
4)
JAX-WS 2.2.6
5) SOAP
UI testing tool.
6) Jboss Application Server 6.1
This first
view of a web service implementation conveys only a simple operation but It is a good first movement to show how the JAX-WS is and
the way that a simple web service can be implemented.
From an
architecture point of view, this very simple example highlights the lightweight
implementation without bearing in mind the contract and contract coding
decoupled approach.
This
article outlines the following stages.
1) Eclipse
Maven project creation.
2) JAX-WS –based web service implementation.
3) Web
Service package generation.
4) Jboss web service deployment.
5)
Verifying the web service deployment
6) Testing the Web Services by using SOAP UI TestSuite.
Description of every step
First stage. Eclipse Maven project creation.
Before creating a new maven project, eclipse maven must be installed thoroughly.
I assume that you have already installed the plugin
with no problems.
In order to create the eclipse project, click the new project and in the
Maven folder select the Maven Project option and click “next” button.
In this case, I have decided to create a non-archetype project;
therefore I have checked the “create a simple project” option.
The result is a new eclipse project respecting the maven directory
structure as following
Second stage. JAX-WS –based web service
implementation.
Firstly, I have added several maven dependencies to the pom.xml maven
file located in the root of the project.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>greeting.services.com</groupId>
<artifactId>GreetingWS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>GreetingWS</name>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
JAX-WS and JUnit dependencies are added. Please, do be aware that in
this case JUnit are not to be used but it is important for future JAX-WS
examples based on this first approach.
Once the file is saved, immediately the maven engine search the
dependencies within the repositories configured and download them into the
local maven repository.
Now, it is time to create the Web service class. For doing this, you can
create a file source like this.
package com.greeting.ws;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style=Style.RPC)
public class GreetingWS {
public String formalGreeting(String name){
return "Hello
boy :" + name;
}
}
As you can see, several issues are worth being commented.
Firstly, the “import” include in the source file the dependencies needed
to use @WebService attribute and @SOABinding.
If you want your class to behave such as a Web service class, you must
use both attributes.
@WebService attribute establishes the class as
a web service in itself.
@SOAPBinding determinates the SOAP style, in
this case RPC style (Document style is permitted as well).
As you can see, you do not need to draw on extra attributes to establish
the methods of this class as web service methods.
Finally, two files are need and you must add to the project under the
WEB-INF folder.
web.xml is the common web specification
file all web-based applications must comprise.
<?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>greetingExample</display-name>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>greetingWs</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>greetingWs</servlet-name>
<url-pattern>/GreetingWS</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>
sun-jaxws.xml is the specific file that defines
the endpoint.
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint name="GreetingWS"
implementation="com.greeting.ws.GreetingWS"
url-pattern="/GreetingWS"
/>
</endpoints>
To access to the web service definition, these files define the
following url.
http://localhost:8080/GreetingWS-0.0.1-SNAPSHOT/GreetingWS?wsld
Third stage. Web Service package generation
Under maven environment, two maven goals must be executed. Firstly, the
compile goal compiles the class and generates the target files.
Next, you must follow the same operation by establishing the “package”
goal. This will generates the WAR file.
GreetingWS-0.0.1-SNAPSHOT.war
Fourth stage. Jboss web
service deployment.
This stage is quite simple. You have just to copy the war file in the
JBOSS_HOME\ server\default\deploy
Then, execute run.bat file located in JBOSS_HOME\bin folder.
Fifth stage. Verifying the web service deployment
By typing the url http://localhost:8080/GreetingWS-0.0.1-SNAPSHOT/GreetingWS?wsld
you can see the web service details.
Sixth stage. Testing the Web
Services by using SOAP UI TestSuite.
The SOAP UI suite provides two modes of use. In this case, it is embedded
in the Eclipse IDE.
The first step is to create a new SOAP UI project.
To create the binding follow the two next steps.
As you can see, the request is created and the web service can be tested
properly.
Submitting the request you obtain the Web service response.
As a conclusion, you can create a simple web service by using JAX-WS and
testing it with SOAP UI test suite.