Thought of creating a web service
project with JAX-WS and Maven. I started this as part of my technology brush up
exercise. This is a very basic I started with. Later will introduce Hibernet,
Spring to this. To start with you should have knowledge of web service, maven
and how to triage if something went wrong.
I am planning to created a web service,
which search user by name, phone, email, address. This web service will develop
step by step.
For now I will show you how to create a
web service with jax-ws, building it with maven and deploying it in JBoss
5.0.1-GA.
1.
Create a new maven web project from eclipse. If you do not have maven
plug-in for eclipse download from <a
href="http://eclipse.org/m2e/download/">m2e-eclipse
project</a>.
2.
Give suitable GroupId and ArtifactId. See below images.
3.
Now you can see the project ServiceSVC in eclipse work space. But,
carefully watching you will notice there is no src/main/java folder created by
the plugin :(. You need to do this by creating a folder 'java' in side
SearchSVC/src/main and then make this as source folder (Build Path -> Use as
source floder).
4.
Create a class SearchServivce in side src/main/java with package as
com.ws.search using JAX-WS.
package com.ws.search;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
*
Hello Search!
*
*/
@WebService(serviceName="SearchService",targetNamespace="http://search/")
public class SearchServivce
{
private
String message = new String("Hello, ");
public
SearchServivce(){
}
@WebMethod(operationName="searchName")
@WebResult(targetNamespace="")
public
String searchName(@WebParam(name="name",targetNamespace="")
String name){
return
message+" "+name+".";
}
}
5.
Now you have to generate few portable class for JAX-WS. You can do it
using maven plugin
Group Id: org.jvnet.jax-ws-commons
Artifact Id: jaxws-maven-plugin
Version: 2.1
This plug-in has 4 goals wsgen, wsgen-test,
wsimport and wsimport-test. For our purpose will use 'wsgen'. POM.xml snippt
<build>
<finalName>SearchSVC</finalName>
<plugins>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsgen</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<genWsdl>true</genWsdl>
<sei>com.ws.search.SearchServivce</sei>
<verbose>true</verbose>
<keep>true</keep>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
6.
Open your web.xml (src/main/resources/WEB-INF) add following.
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"
>
<web-app>
<display-name>SearchService Webservice</display-name>
<servlet>
<servlet-name>SearchService</servlet-name>
<servlet-class>com.ws.search.SearchServivce</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearchService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
7.
Remove index.jsp which created by maven webapp archtype.
8.
Final pom.xml.
<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>com.ws.search</groupId>
<artifactId>SearchService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SearchService</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>SearchSVC</finalName>
<plugins>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsgen</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<genWsdl>true</genWsdl>
<sei>com.ws.search.SearchServivce</sei>
<verbose>true</verbose>
<keep>true</keep>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
9. From eclipse click run as maven to
build your project OR from command prompt give mvn clean package.
10. If in your eclipse work space you
see this below problem 'Description Resource Path Location Type Plugin execution not covered by
lifecycle configuration: org.jvnet.jax-ws-commons:jaxws-maven-plugin:2.1:wsgen
(execution: default, phase: process-classes) pom.xml /SearchSVC
line 26 Maven
Project Build Lifecycle Mapping Problem'.
Add the following in your pom.xml inside the
<build> tag
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<versionRange>[2.1,)</versionRange>
<goals>
<goal>wsgen</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
11. Copy the WAR from SearchSVC/target/
and paste it inside {JBOSS_HOME}/server\default\deploy.
12. Before start the server you need to
do the following changes in JBOSS to get rid of this exception
'javax.xml.ws.WebServiceException: No Content-type in the header!'.
The below libraries (found in
${JBOSS_HOME}/common/lib) had to be moved to the endorsed folder
(${JBOSS_HOME}/lib/endorsed).
jbossws-native-jaxrpc.jar
jbossws-native-jaxws.jar
jbossws-native-jaxws-ext.jar
jbossws-native-saaj.jar
13. start the server if your service
deployment successful you can see the following in your console
17:20:27,296 INFO [TomcatDeployment] deploy, ctxPath=/SearchSVC
17:20:27,421 INFO [WSDLFilePublisher] WSDL published to:
file:/C:/jboss-5.0.1.GA/server/default/data/wsdl/SearchSVC.war/SearchService147934348314496462.wsdl
14. Then hit <a
href="http://localhost:8080/SearchSVC?wsdl">http://localhost:8080/SearchSVC?wsdl</a>.
You can see the wsdl.