JAX-WS + Spring integration example

Here’s a guide to show you how to integrate Spring with JAX-WS, as mention in this link : http://jax-ws-commons.java.net/spring/. Upon finishing this tutorial, you will create a simple HelloWorld web service (JAX-WS), and DI a bean into the web service via Spring.

1. Project Folder

See the final project folder structure.

jaxws-spring-folder-structure

2. Project Dependencies

Use Maven to get all the library dependencies. The key to integrate Spring with JAX-WS is via jaxws-spring.jar.

File : 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mkyong</groupId>
  <artifactId>WebServicesExample</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>WebServicesExample Maven Webapp</name>
  <url>http://maven.apache.org</url>
 
  <repositories>
    <repository>
      <id>java.net</id>
      <url>http://download.java.net/maven/2</url>
    </repository>
  </repositories>
 
  <dependencies>
 
        <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
        </dependency>
 
	<!-- Spring framework --> 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring</artifactId>
		<version>2.5.6</version>
	</dependency>
 
        <!-- JAX-WS --> 
	<dependency>
	  	<groupId>com.sun.xml.ws</groupId>
	        <artifactId>jaxws-rt</artifactId>
	        <version>2.2.3</version>
	</dependency>
 
 	<!-- Library from java.net, integrate Spring with JAX-WS --> 
	<dependency>
		<groupId>org.jvnet.jax-ws-commons.spring</groupId>
		<artifactId>jaxws-spring</artifactId>
		<version>1.8</version>
		<exclusions>
		  <exclusion>
           		<groupId>org.springframework</groupId>
      			<artifactId>spring-core</artifactId>
        	  </exclusion>
        	  <exclusion>
           		<groupId>org.springframework</groupId>
      			<artifactId>spring-context</artifactId>
        	  </exclusion>
        	  <exclusion>
           		<groupId>com.sun.xml.stream.buffer</groupId>
      			<artifactId>streambuffer</artifactId>
        	  </exclusion>
        	  <exclusion>
           		<groupId>org.jvnet.staxex</groupId>
      			<artifactId>stax-ex</artifactId>
        	  </exclusion>
		</exclusions>
	</dependency>
 
  </dependencies>
  <build>
    <finalName>web services</finalName>
    <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>2.3.1</version>
           <configuration>
               <source>1.6</source>
               <target>1.6</target>
           </configuration>
       </plugin>
    </plugins>
  </build>
</project>
Note
The jaxws-spring’s pom.xml has a lot of unnecessary dependencies, you may need to exclude it via </exclusions> tag.

3. JAX-WS Hello World

A simple JAX-WS example, and dependency inject (DI) “HelloWorldBo” via Spring.

File : HelloWorldWS.java

package com.mkyong.ws;
 
import javax.jws.WebMethod;
import javax.jws.WebService;
 
import com.mkyong.bo.HelloWorldBo;
 
@WebService
public class HelloWorldWS{
 
	//DI via Spring
	HelloWorldBo helloWorldBo;
 
	@WebMethod(exclude=true)
	public void setHelloWorldBo(HelloWorldBo helloWorldBo) {
		this.helloWorldBo = helloWorldBo;
	}
 
	@WebMethod(operationName="getHelloWorld")
	public String getHelloWorld() {
 
		return helloWorldBo.getHelloWorld();
 
	}
 
}

4. Beans

Here’s the HelloWorldBo class, with a getHelloWorld() method to return a simple string.

File : HelloWorldBo.java

package com.mkyong.bo;
 
public interface HelloWorldBo{
 
	String getHelloWorld();
 
}

File : HelloWorldBoImpl.java

package com.mkyong.bo.impl;
 
import com.mkyong.bo.HelloWorldBo;
 
public class HelloWorldBoImpl implements HelloWorldBo{
 
	public String getHelloWorld(){
		return "JAX-WS + Spring!";
	}
 
}

5. Spring Beans Configuration

Spring beans configuration file to bind URL pattern “/hello” to “com.mkyong.ws.HelloWorldWS” web service class.

File : applicationContext.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.5.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"
>
 
    <wss:binding url="/hello">
        <wss:service>
            <ws:service bean="#helloWs"/>
        </wss:service>
    </wss:binding>
 
    <!-- Web service methods -->
    <bean id="helloWs" class="com.mkyong.ws.HelloWorldWS">
    	<property name="helloWorldBo" ref="HelloWorldBo" />
    </bean>
 
    <bean id="HelloWorldBo" class="com.mkyong.bo.impl.HelloWorldBoImpl" />
 
</beans>
Note
With this jaxws-spring integration mechanism, the sun-jaxws.xml file is no longer required.

6. web.xml

In web.xml, declares “com.sun.xml.ws.transport.http.servlet.WSSpringServlet“, and link it to “/hello“.

<web-app id="WebApp_ID" version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
	<display-name>Spring + JAX-WS</display-name>
 
	<servlet>
    	        <servlet-name>jaxws-servlet</servlet-name>
    	       <servlet-class>
    		          com.sun.xml.ws.transport.http.servlet.WSSpringServlet
    	        </servlet-class>
  	</servlet>
 
	<servlet-mapping>
                <servlet-name>jaxws-servlet</servlet-name>
                <url-pattern>/hello</url-pattern>
         </servlet-mapping>
 
         <!-- Register Spring Listener -->
  	<listener>
    	        <listener-class>
    		     org.springframework.web.context.ContextLoaderListener
    	        </listener-class>
  	</listener>
 
</web-app>

7. Demo

Start the project, and access the deployed web service via URL “/hello“, for example http://localhost:8080/WebServicesExample/hello?wsdl

jaxws-spring-demo

Reference

  1. JAX-WS + Java Web Application Integration Example
  2. http://jax-ws-commons.java.net/spring/
  3. http://weblogs.java.net/blog/kohsuke/archive/2007/01/spring_support.html
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: JAVA – Como criar um WebService JAX-WS com SPRING 3 | iDeIAS()

  • Pingback: JAVA – Criando um WebService com SPRING | iDeIAS()

  • Pingback: Spring – SOAP Web Service IntegrationCopyQuery CopyQuery | Question & Answer Tool for your Technical Queries,CopyQuery, ejjuit, query, copyquery, copyquery.com, android doubt, ios question, sql query, sqlite query, nodejsquery, dns query, update()

  • JavaCoder

    How can I get the XSD (or schema) inline instead of it pointing to the URL?

    Thanks in advance.

  • Uj

    Hi,

    I have downloaded the zip file and edited xsd locations as mentioned above but still I am getting org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; systemId: http://jax-ws.java.net/spring/servlet.xsd; lineNumber: 1; columnNumber: 1; Premature end of file when I run on the tomcat server

  • cg

    This is the 5th or 6th example/tutorial I am following from your side.

    In most of these you follow a pattern in which in the first step you show the directory structure.

    It seems from the eclipse ide. However, you dont mention which option you took to create the project. I mean particularly for this example: was this project created as “dynamic web project” or “web service” or “maven” or something else.

    I normally try to create everything manually if not specified. As in this example, i created folders and then files in it. I tried to build by mvn install.

    So even though this is a good informative example, it didnt work for me after spending a day.

    It would be great if in your tutorials, you can add the steps to start, build and deploy too.

    cg

  • KingDevil

    I followed the guide and run on client successful. But when I used Android device to call the webservice. I never put param to method on webservice. It’s always null value? Can you help me fix it?

  • Yingjie

    Compile and package successful; and after deploy on my tomcat, the wsdl file cannot be displayed. http://localhost:8080/WebServicesExample/hello?wsdl

    BTW, the http://jax-ws.dev.java.net/spring/core http://jax-ws.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.java.net/spring/servlet.xsd” have already been modified.b

    Could you please tell me how could you fix it?

    Thank you!

  • Steve

    Hi there, thanks for the awesome tutorial!!

    I am trying to print the request xml sent and the response recived from the Webservice on to my log and am following the instructions give here: http://static.springsource.org/spring-ws/site/reference/html/common.html#logging

    However, despite many tries, I have had no luck in printing the xml to the logs. Any ideas?

  • Jatin Sharma

    Very Useful.

  • Ionut Craciun

    Hi guys,
    It seems that cannot be deployed in Tomcat 6, I get the following error:

    10.06.2013 16:15:44 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing [email protected]7461373f: display name [Root WebApplic
    ationContext]; startup date [Mon Jun 10 16:15:44 EEST 2013]; root of context hierarchy
    10.06.2013 16:15:44 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
    10.06.2013 16:15:45 org.springframework.web.context.ContextLoader initWebApplicationContext
    SEVERE: Context initialization failed
    org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from ServletContext resour
    ce [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Premature end of fil
    e.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.j
    ava:404)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.jav
    a:342)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.jav
    a:310)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefini
    tionReader.java:143)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefini
    tionReader.java:178)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefini
    tionReader.java:149)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext
    .java:124)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext
    .java:92)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefresha
    bleApplicationContext.java:123)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationCont
    ext.java:422)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
    at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1385)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:306)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1389)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1653)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1642)
    at java.lang.Thread.run(Thread.java:662)
    Caused by: org.xml.sax.SAXParseException: Premature end of file.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:
    195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1427)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:
    1056)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScanne
    rImpl.java:511)
    at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaParsingConfig.parse(SchemaParsingConfig.java:435)
    at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaParsingConfig.parse(SchemaParsingConfig.java:491)
    at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaDOMParser.parse(SchemaDOMParser.java:510)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument(XSDHandler.java:1802)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:531)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:556)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(XMLSchemaValidator.java:2443)

    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1788
    )
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:711)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.ja
    va:400)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocument
    FragmentScannerImpl.java:2756)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScanne
    rImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.j
    ava:396)
    … 29 more

    The applicationContext.xml is just as in the example but my own packages:

    Can you please help?

    Best regards,
    Ionut

    • Ionut Craciun

      This is the WEB-INF/contextApplication.xml file:

  • Yasser

    Are you sure that you’ve mentioned all dependencies?! I created exact same small project but it says:

    java.lang.ClassCastException: Cannot cast com.sun.xml.ws.security.impl.policy.SecurityPolicyValidator to com.sun.xml.ws.policy.spi.PolicyAssertionValidator
    at java.lang.Class.cast(Class.java:3005)
    at com.sun.xml.ws.policy.privateutil.ServiceFinder$LazyIterator.next(ServiceFinder.java:382)
    at com.sun.xml.ws.policy.privateutil.ServiceFinder.toArray(ServiceFinder.java:232)
    at com.sun.xml.ws.policy.privateutil.PolicyUtils$ServiceProvider.load(PolicyUtils.java:455)
    at com.sun.xml.ws.policy.AssertionValidationProcessor.(AssertionValidationProcessor.java:85)
    at com.sun.xml.ws.api.policy.ValidationProcessor.(ValidationProcessor.java:73)
    at com.sun.xml.ws.api.policy.ValidationProcessor.getInstance(ValidationProcessor.java:83)
    at com.sun.xml.ws.policy.jaxws.DefaultPolicyResolver.validateServerPolicyMap(DefaultPolicyResolver.java:88)
    at com.sun.xml.ws.policy.jaxws.DefaultPolicyResolver.resolve(DefaultPolicyResolver.java:69)
    at com.sun.xml.ws.policy.jaxws.PolicyWSDLParserExtension.postFinished(PolicyWSDLParserExtension.java:970)
    at com.sun.xml.ws.wsdl.parser.DelegatingParserExtension.postFinished(DelegatingParserExtension.java:191)
    at com.sun.xml.ws.wsdl.parser.WSDLParserExtensionFacade.postFinished(WSDLParserExtensionFacade.java:338)
    at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:338)
    at com.sun.xml.ws.server.EndpointFactory.getWSDLPort(EndpointFactory.java:645)
    at com.sun.xml.ws.server.EndpointFactory.create(EndpointFactory.java:280)
    at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:147)
    at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:574)
    at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:557)
    at org.jvnet.jax_ws_commons.spring.SpringService.getObject(SpringService.java:333)
    at org.jvnet.jax_ws_commons.spring.SpringService.getObject(SpringService.java:45)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:109)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:274)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:125)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4334)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

    It seems it needs webservices-rt.jat but after that it again says another error!!! :((

  • Giuseppe Annunziata

    I’d like to warning about URLs for schema in spring configuration.
    These now are:
    http://jax-ws.java.net/spring/servlet.xsd
    and
    http://jax-ws.java.net/spring/core.xsd
    (but namespace is still http://jax-ws.dev.java.net/spring/core and
    http://jax-ws.dev.java.net/spring/servlet)

    • Amit

      Hi,

      I tried to compile but getting error in spring config file for wss binding :

      Error :

      Multiple annotations found at this line:
      – schema_reference.4: Failed to read schema document ‘http://jax-ws.dev.java.net/spring/servlet.xsd’, because 1) could not
      find the document; 2) the document could not be read; 3) the root element of the document is not .
      – cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ‘wss:binding’.

  • michaeljoi

    I don’t know if this is a bug or not.

    http://localhost:8080/WebServicesExample/hello?wsdl

    Won’t give out the result as mentioned. I have to modify the pom.xml to be like this:
    WebServicesExample

    Or else it just won’t find the wsdl.

    • Yingjie

      Hi michaeljoi,

      I met the problem as you met. Compile and package successful; and after deploy on my tomcat, the wsdl file cannot be displayed. Could you please tell me how could you fix it?

      Thank you!

  • Pradip Bhatt

    Hi sir…
    I am created different modules using Struts2 + Spring3.0 + Hibernate – JPA. Now my question is that How can I connect each independent module using web Service especially Spring Web Service.

    Can Any one is helping me on this???

    Thankssssssssss in Advance.

  • Gurditta

    How would i create client to call this web service.?

    thanks

  • Madav

    In pom.xml getting the below error

    cvc-complex-type.2.4.c: The matching wildcards is strict, but no declaration can be found for element ‘wss:binding’

    at line:

    What is the problem for this one?

  • will

    So, I’ve got it all working, and running. But what url do I enter in the browser so that the method :

    HelloWorldBoImpl::getHelloWorld() is called, which should return

    “JAX-WS + Spring!”

    ?

  • CHris

    Hello!

    Could you do a Web Services integration via SPRING + Apache CXF? I mean Servicemix +Spring

  • Subcomandante_M

    Works like a charm … Thx a lot!

  • Maxim

    this works only for such simple example.
    if you try to do something further – lots or errors

  • anudeep

    nice work ..

  • john

    I’m getting a null pointer when i try to create a soapUI project to this webservice.
    Have you any suggestion to this problem?

  • neha

    I want jax ws +spring +hibernate example with database integration any help plz.

    • neha

      No it is not possible.

  • neha

    I want jax ws +spring +hibernate example with database integration any one help me plz…..

  • Panneer

    How to use my own creation of wsdl file in Apachi CXF web service?

  • Nandhu

    sorry to include this…. am running apache tomcat 7.0.30

  • Nandhu

    No exception when running but when i goto http://localhost:8080/WebServicesExample/hello?wsdl its giving me 404 not found error… help me rectify this problem

  • http://newjazz.net Gilbert Engle

    I am trying to get things working on JBoss. Parsing applicationContext.xml keeps failing. I pass in the latest Spring jar files into JBoss via -classpath but it seems like it’s using old jars. I have read that you can have problems parsing applicationContext.xml with old Spring jar files. I have tried EVERYTHING to get past this parse error. Nothing is working. Has anybody else got this working on JBoss?

    14:44:27,408 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-
    host].[/kkkkkk]] (MSC service thread 1-7) Exception sending context initialized
    event to listener instance of class org.springframework.web.context.ContextLoade
    rListener: org.springframework.beans.factory.BeanDefinitionStoreException: Line
    11 in XML document from class path resource [applicationContext.xml] is invalid;
    nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber
    : 57; Document root element “beans”, must match DOCTYPE root “null”.

  • rushit

    SEVERE: Context initialization failed
    org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 28 in XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘property’. One of ‘{“http://www.springframework.org/schema/beans”:import, “http://www.springframework.org/schema/beans”:alias, “http://www.springframework.org/schema/beans”:bean, WC[##other:”http://www.springframework.org/schema/beans”]}’ is expected.

  • Leonardo

    A god damm trouble do deal with dependencies, and jboss, and jdk 6 and everything

    but after a lot of trouble, a great solution for web services

    thanks

  • Michael

    Hi,

    I am attempting to deploy a JAX-WS service like this using WSSpringServlet and instead of wiring my beans in using XML I am attempting to use @Autowired with no success. Is there any reason you can think of this shouldn’t work, or do I need to load something else from spring to make autowiring work?

    Thanks

  • Juan

    Hi mkyong , I have working fine these with Tomcat. Do you know if JAX-WS + Spring work also with weblogic 10.

    I am trying but I could get it works.

    Any help/link is welcome.

    Cheers.

  • chris

    hello
    thx for the tutorial – checked it out on apache tomcat but get this exception:
    java.lang.ClassCastException: com.sun.xml.ws.transport.http.servlet.WSSpringServlet cannot be cast to javax.servlet.Servlet
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1116)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:809)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:680)

    any idea?
    thx
    cheers
    c

    • Rakesh

      Hi Chris, I am also facing the same problem “java.lang.ClassCastException: com.sun.xml.ws.transport.http.servlet.WSSpringServlet cannot be cast to javax.servlet.Servlet”. Have you find the solution?

    • imran khan

      I just add below in exclusion list and it worked:-

      javax.servlet
      servlet-api

      • imran khan
        &lt;exclusion&gt;
        	&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
        	&lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
        &lt;/exclusion&gt;
  • Zeus

    Excellent tutorial!!

    MK, when you have some free time, could you please expand on this example by providing another web application that consumes the data as a client?

    This would really help give the bigger picture.

    Thanks much!

    you are a pioneer in this industry!

  • viswa

    Hi , when I am working on this example, while writing the applicationContext.xml file
    it is giving error as

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
           xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
           xmlns:ws=&quot;http://jax-ws.dev.java.net/spring/core&quot;
           xmlns:wss=&quot;http://jax-ws.dev.java.net/spring/servlet&quot;
           xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.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&quot;
    &gt;
     
        &lt;wss:binding url=&quot;/hello&quot;&gt;
            &lt;wss:service&gt;
                &lt;ws:service bean=&quot;#helloWs&quot;/&gt;
            &lt;/wss:service&gt;
        &lt;/wss:binding&gt;
     
        &lt;!-- Web service methods --&gt;
        &lt;bean id=&quot;helloWs&quot; class=&quot;com.mkyong.ws.HelloWorldWS&quot;&gt;
        	&lt;property name=&quot;helloWorldBo&quot; ref=&quot;HelloWorldBo&quot; /&gt;
        &lt;/bean&gt;
     
        &lt;bean id=&quot;HelloWorldBo&quot; class=&quot;com.mkyong.bo.impl.HelloWorldBoImpl&quot; /&gt;
     
    &lt;/beans&gt;

    Multiple annotations found at this line:
    – Start tag of element
    – cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element
    ‘wss:binding’.

    • davduke

      I have the same problem and i can’t find the solution. Many people have the same but nobody solved it.

      It seems to be something wrong with the url of the schemas, but i couldn’t find the new ones.

      • akee

        Make sure you have the xbean-spring-2.8.jar in the lib. I did this with Ant (can’t use maven due to firewall block to repo), that’s the error I got when I don’t have this jar file. Very misleading error as I thought it’s schema related.

  • june

    Works perfectly on tomcat. I was trying to deploy it on Websphere but got an error. It can’t start the application. Can you please direct me how to change this project so that I can deploy in websphere.

    A client sample to call this will be of great help too. Thanks in advance gurus.

  • Vijay

    A very good example. Gives all the startup information to build JAXWS-Spring webservices. And It works perfectly. :)

  • sakshi

    Hi,
    I did an mvn install for the above example, and then I just deployed the war file obtained to glassfish. But when i tried to access the wsdl url u mention(http://localhost:8080/WebServicesExample/hello?wsdl), I get an error 404 page not found.Can anyone tell me why this might be happening?

    • marcosEgatz

      Hi,
      I’ve just landed to this great source of help and I’ve also experienced your 404 error. I solved it by poiting the browser to “http://localhost:8084/hello?wsdl”. Take a look at “http://localhost:8084/hello”, and you’ll see the reference to the wsld.
      P.D.: The application was deployed in a Tomcat 7.0.22.
      regards

  • Sasika

    Thanks.Most of your Examples very helpful.But this i got this exception.

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.sun.xml.ws.transport.http.servlet.SpringBinding#0' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot create inner bean '(inner bean)' of type [org.jvnet.jax_ws_commons.spring.SpringService] while setting bean property 'service'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': FactoryBean threw exception on object creation; nested exception is java.lang.NoClassDefFoundError: com/sun/xml/bind/api/JAXBRIContext
            at 
    Caused by: java.lang.NoClassDefFoundError: com/sun/xml/bind/api/JAXBRIContext
            at com.sun.xml.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:203)
            at com.sun.xml.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:343)
            at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:205)
            at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:513)
            at org.jvnet.jax_ws_commons.spring.SpringService.getObject(SpringService.java:333)
            at org.jvnet.jax_ws_commons.spring.SpringService.getObject(SpringService.java:45)
            at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
            ... 53 more
    Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.api.JAXBRIContext
            at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
            at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)
            ... 60 more
    • Harsha

      I believe you are getting a class not found exception. Easy to fix.

      Besides that, this is a very good tutorial.

      • Sasika

        Thanks Harsha.I added some extra libraries and it’s work now.
        Yes this is very good example.

      • Sasika

        Hi Harsha it’s me again.
        This example work fine with Apache Tomcat and Glassfish servers.
        I have run this on Jboss 6 server.
        But it’s not working with jboss 6.
        Why this is not working with jboss 6?

  • Javier

    Thanks a lot for you example, now I’m trying to make it more secure with wss, can you explain more about it please??.. thanks

    • http://www.mkyong.com mkyong

      Sorry, no familiar with wss. Implemented authentication with SSL before, please share your find out on wss in future :)

  • kray

    Just wanted to share my experience with this example.

    I downloaded zip file, opened in Netbeans 6.9.1, compiled successfully, deployed through Tomcat 6 manager page. The final name for the war file in the pom.xml is “web service” so the war file is “web service.war” and when tomcat deploys it get deployed under the name “web services” instead of “WebServicesExample” as the demo link has. I changed the name of the finalName parameter in the pom.xml file to be “WebServicesExample”, recompiled and redeployed. The demo link worked after that.