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;

import uk.co.mo.training.dto.Weather;

@SuppressWarnings("restriction")
@WebService
public interface TemperatureService {
	
	@WebMethod
	public Weather toCelsius(@WebParam(name="fahrenheit") double fahrenheit);
	@WebMethod
	public Weather toFahrenheit(@WebParam(name="celsius") double celsius);

}

//Weather.java
package uk.co.mo.training.dto;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@SuppressWarnings("restriction")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Weather", propOrder = { "city", "celsius" , "fahrenheit"})
@XmlRootElement(name = "Weather")
public class Weather {
	
	@XmlElement(name = "Ciy", required = false)
	private String city;

	@XmlElement(name = "Celsius", required = true)
	private double celsius;
	@XmlElement(name = "Fahrenheit", required = true)
	private double fahrenheit;
		
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public double getCelsius() {
		return celsius;
	}
	public void setCelsius(double celsius) {
		this.celsius = celsius;
	}
	public double getFahrenheit() {
		return fahrenheit;
	}
	public void setFahrenheit(double fahrenheit) {
		this.fahrenheit = fahrenheit;
	}
	
}

(2) Define service Implementation

//TemperatureServiceImple .java
package uk.co.mo.training.ws;

import javax.jws.WebService;

import org.springframework.stereotype.Service;

import uk.co.mo.training.dto.Weather;

@SuppressWarnings("restriction")
@WebService(endpointInterface = "uk.co.mo.training.ws.TemperatureService",serviceName="temperatureService")
@Service
public class TemperatureServiceImple implements TemperatureService {


	public Weather toCelsius(double fahrenheit) {
		double celsius = (5.0 / 9.0) * (fahrenheit - 32.0);
		//C° = (5 / 9)x (F° - 32)  
		
		Weather w = new Weather();
		w.setCelsius(celsius);
		w.setFahrenheit(fahrenheit);
		return w;
	}


	public Weather toFahrenheit(double celsius) {
		double fahrenheit = (celsius *1.8)+32.0;
		//F° = (C° x 1.8) + 32
		Weather w = new Weather();
		w.setCelsius(celsius);
		w.setFahrenheit(fahrenheit);
		return w;
	}

}

(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
OR
http://localhost:8080/mo-ws/webservice/temperaturewebservice

OR
DEMO
http://env-7398619.j.layershift.co.uk/ws/webservice/temperaturewebservice
(10) Using soapUI test web service

(11) Download Source Code mo-ws.zip or war file at mo-ws.war

Advertisements
  1. j2eefan
    June 23, 2011 at 07:58

    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

    Very valueable tutorial. Thank you!

  3. jAlex
    August 8, 2011 at 10:02

    Really useful; thank

  4. nailAderson
    August 8, 2011 at 14:15

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

  5. oussa
    September 25, 2011 at 19:11

    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

      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

  6. KwaXi
    November 16, 2012 at 08:00

    does this work for spring 3.1, too? jaxws-spring depends on spring 2.0.

  7. Chitra
    January 23, 2013 at 23:50

    Hi,

    Your tutorial works fine. But when I tried to create similar service to print name, I am getting below exception

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘(inner bean)’ defined in ServletContext resource [/WEB-INF/conf/ws-context.xml]: Cannot resolve reference to bean ‘CustomerServiceImpl’ while setting bean property ‘bean’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘CustomerServiceImpl’ is defined
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:270)

    Please provide your suggestion to resolve this issue.

  8. February 20, 2013 at 04:49

    I really desire to book mark this specific posting, “Developing Web Services using jax-ws,
    spring and maven Abdul Aziz” on my own blog. Do you mind in
    the event Ido it? Thanks ,Jeanne

  9. June 5, 2013 at 09:06

    INFO]
    [INFO] — maven-war-plugin:2.1.1:war (default-war) @ mo-ws —
    [INFO] Packaging webapp
    [INFO] Assembling webapp [mo-ws] in [C:\Documents and Settings\abdula\My Documents\Downloads\mo-ws\mo-ws\target\mo-ws]
    [INFO] Processing war project
    [INFO] Copying webapp resources [C:\Documents and Settings\abdula\My Documents\Downloads\mo-ws\mo-ws\src\main\webapp]
    [INFO] Webapp assembled in [1250 msecs]
    [INFO] Building war: C:\Documents and Settings\abdula\My Documents\Downloads\mo-ws\mo-ws\target\mo-ws.war
    [WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored
    (webxml attribute is missing from war task, or ignoreWebxml attribute is specified as ‘true’)
    [INFO]
    [INFO] — jaxws-maven-plugin:1.10:wsgen (WeatherWS) @ mo-ws —
    Note: ap round: 1
    [ProcessedMethods Interface: uk.co.mo.training.ws.WeatherService]
    [should process method: currentWeather hasWebMethods: false ]
    [endpointReferencesInterface: true]
    [declaring class has WebSevice: true]
    [returning: true]
    [WrapperGen – method: currentWeather(java.lang.String)]
    [method.getDeclaringType(): uk.co.mo.training.ws.WeatherService]
    [requestWrapper: uk.co.mo.training.ws.jaxws.CurrentWeather]
    uk\co\mo\training\ws\jaxws\CurrentWeather.java
    uk\co\mo\training\ws\jaxws\CurrentWeatherResponse.java
    Note: ap round: 2
    [INFO]
    [INFO] — jaxws-maven-plugin:1.10:wsgen (TemperatureWS) @ mo-ws —
    Note: ap round: 1
    [ProcessedMethods Interface: uk.co.mo.training.ws.TemperatureService]
    [should process method: toCelsius hasWebMethods: false ]
    [endpointReferencesInterface: true]
    [declaring class has WebSevice: true]
    [returning: true]
    [WrapperGen – method: toCelsius(double)]
    [method.getDeclaringType(): uk.co.mo.training.ws.TemperatureService]
    [requestWrapper: uk.co.mo.training.ws.jaxws.ToCelsius]
    [should process method: toFahrenheit hasWebMethods: false ]
    [endpointReferencesInterface: true]
    [declaring class has WebSevice: true]
    [returning: true]
    [WrapperGen – method: toFahrenheit(double)]
    [method.getDeclaringType(): uk.co.mo.training.ws.TemperatureService]
    [requestWrapper: uk.co.mo.training.ws.jaxws.ToFahrenheit]
    uk\co\mo\training\ws\jaxws\ToCelsius.java
    uk\co\mo\training\ws\jaxws\ToCelsiusResponse.java
    uk\co\mo\training\ws\jaxws\ToFahrenheit.java
    uk\co\mo\training\ws\jaxws\ToFahrenheitResponse.java
    Note: ap round: 2
    [INFO] ————————————————————————
    [INFO] BUILD SUCCESS
    [INFO] ————————————————————————
    [INFO] Total time: 9.703s
    [INFO] Finished at: Wed Jun 05 09:56:49 BST 2014
    [INFO] Final Memory: 18M/245M
    [INFO] ————————————————————————

  10. July 12, 2014 at 09:51

    I know this if off topic but I’m looking into starting my own blog and was curious what all is
    needed to get set up? I’m assuming having a
    blog like yours would cost a pretty penny?
    I’m not very internet smart so I’m not 100% certain. Any
    tips or advice would be greatly appreciated. Cheers

  1. August 14, 2011 at 21:07
  2. November 5, 2012 at 01:36

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: