Developing Web Services using jax-ws, spring and maven
June 15, 2011
7 comments
Using the following steps you can develop soap Web Services using jax-ws, spring and maven plugin
- Define service interface
- Define service Implementation
- Web.xml – add JAX-WS commons spring ext servlet
- Spring beans configuration using annotations
- Spring configurations for Web Services
- pom.xml – Add ‘jaxws-maven-plugin’ plugin
- Build deployment using maven
- Check the web service
- Test web service using sopaUI
- Download Source Code mo-ws.zip
or war file mo-ws.war
(1) Define service interface
//TemperatureService .java
package uk.co.mo.training.ws;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@SuppressWarnings("restriction")
@WebService
public interface TemperatureService {
@WebMethod
public double toCelsius(@WebParam(name="fahrenheit") double fahrenheit);
@WebMethod
public double toFahrenheit(@WebParam(name="celsius") double celsius);
}
(2) Define service Implementation
//TemperatureServiceImple .java
package uk.co.mo.training.ws;
import javax.jws.WebService;
import org.springframework.stereotype.Service;
@SuppressWarnings("restriction")
@WebService(endpointInterface = "uk.co.mo.training.ws.TemperatureService",serviceName="temperatureService")
@Service
public class TemperatureServiceImple implements TemperatureService {
public double toCelsius(double fahrenheit) {
double celsius = (5.0 / 9.0) * (fahrenheit - 32.0);
//C° = (5 / 9)x (F° - 32)
return celsius;
}
public double toFahrenheit(double celsius) {
double fahrenheit = (celsius *1.8)+32.0;
//F° = (C° x 1.8) + 32
return fahrenheit;
}
}
(3) Web.xml – add JAX-WS commons spring ext servlet
<!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>
<!-- spring framework context configuration -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/main-application-context.xml</param-value>
</context-param>
<!-- Spring Context Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- this is for JAX-WS commons spring ext. -->
<servlet>
<servlet-name>jaxws-servlet</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSSpringServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jaxws-servlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
(4) Spring beans configuration using annotations
<!-- main-application-context.xml-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!—scan annotations -->
<context:component-scan base-package="uk.co.mo.training.ws"/>
<context:annotation-config/>
<!-- Web Services -->
<import resource="ws-context.xml"/>
</beans>
(5) Spring configurations for Web Services
<!-- ws-context.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://jax-ws.dev.java.net/spring/core
http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
http://jax-ws.dev.java.net/spring/servlet.xsd">
<!-- Temperature Web Service -->
<wss:binding url="/webservice/temperaturewebservice">
<wss:service>
<ws:service bean="#temperatureServiceImple">
</ws:service>
</wss:service>
</wss:binding>
</beans>
(6) pom.xml – Add ‘jaxws-maven-plugin’ plugin
<!-- pom.xml-->
.....
<build>
<finalName>mo-ws</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>TemperatureWS</id>
<configuration>
<sei>uk.co.mo.training.ws.TemperatureServiceImple</sei>
<genWsdl>true</genWsdl>
<keep>true</keep>
<verbose>true</verbose>
</configuration>
<phase>package</phase>
<goals>
<goal>wsgen</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
....
(7) Build deployment using maven: Run (1) “mvn clean package” and again (2) “mvn package”
"mvn clean package" - it will create required WSDL files at "/target/jaxws/wsgen/wsdl" "mvn package" it will create some files at "/target/mo-ws/WEB-INF/classes/uk/co/mo/training/ws/jaxws"
(8) Deploy in the tomcat
<!-- server.xml--> ... <Context docBase="D:/projects/mo-ws-project/target/mo-ws" path="/ws"> </Context> ...
(9) Check the web service http://localhost:8080/ws/webservice/temperaturewebservice
(10) Using soapUI test web service
(11) Download Source Code mo-ws.zip or war file at mo-ws.war