Home > Integration-with-Enterprise-Services, Spring Framework > Developing Web Services using jax-ws, spring and maven

Developing Web Services using jax-ws, spring and maven

Using the following steps you can develop soap Web Services using jax-ws, spring and maven plugin

  1. Define service interface
  2. Define service Implementation
  3. Web.xml – add JAX-WS commons spring ext servlet
  4. Spring beans configuration using annotations
  5. Spring configurations for Web Services
  6. pom.xml – Add ‘jaxws-maven-plugin’ plugin
  7. Build deployment using maven
  8. Check the web service
  9. Test web service using sopaUI
  10. 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

  1. j2eefan
    June 23, 2011 at 07:58 | #1

    The @WebParam annotation is necessary as java interfaces do not store the parameter names in the .class files.

    So if you leave out the annotation your parameter will be named arg0.

  2. Sebi
    July 13, 2011 at 12:29 | #2

    Very valueable tutorial. Thank you!

  3. jAlex
    August 8, 2011 at 10:02 | #3

    Really useful; thank

  4. nailAderson
    August 8, 2011 at 14:15 | #4

    By extending SpringBeanAutowiringSupport, you can annotate an
    endpoint’s properties with @Autowired

  5. oussa
    September 25, 2011 at 19:11 | #5

    thank you for this tutorial !! but i have this error :

    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate NamespaceHandler for namespace [http://www.springframework.org/schema/context]
    Offending resource: ServletContext resource [/WEB-INF/conf/main-application-context.xml]

    • abdul
      September 26, 2011 at 09:01 | #6

      This is because of a missing spring jar file; please check maven jar file. Build deployment using maven and check all the dependencies are there

  1. August 14, 2011 at 21:07 | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.