Developing a simple SOAP-webservice using Spring 3.0.1 and Apache CXF 2.2.6

Phillip Steffensen's picture

In the past few years many techniques have been developed to help applications interact with each other. One of them are webservice-interfaces. These interfaces are extremly popular in the world of Java software development. One Framework that can be used to build such interfaces is Apache CXF. Apache CXF delivers a toolset to develop interfaces using different protocols like XML/HTTP, RESTful HTTP, Corba and SOAP. In this article i'd like to show how easy it could be to develop a simple SOAP-webservice based on Apache CXF 2.2.6 and the Spring Framework 3.0.1. You can download the full example at the bottom of this article.

Setting up the project...

First I set up a small Maven 2 project that packages my application as a web application archive (*.war-file). Therefore I created the following pom.xml:

  1. <project xmlns="...">
  2.     <modelVersion>4.0.0</modelVersion>
  3.     <groupId>com.unitedcoders.demo</groupId>
  4.     <artifactId>CXFExampleService</artifactId>
  5.     <packaging>war</packaging>
  6.     <version>0.0.1-SNAPSHOT</version>
  7.     <name>Example Apache CXF Webservice</name>
  8.     <url>http://united-coders.com</url>
  9.    
  10.     <!-- Dependency properties -->
  11.     <properties>
  12.         <junit-version>4.5</junit-version>
  13.         <cxf.version>2.2.6</cxf.version>
  14.         <spring-version>3.0.1.RELEASE</spring-version>
  15.         <commons-logging-version>1.1.1</commons-logging-version>
  16.     </properties>
  17.    
  18.     <!-- Plugin configuration -->
  19.     <build>
  20.         <finalName>CXFExampleService</finalName>
  21.         <plugins>
  22.             <plugin>
  23.                 <groupId>org.apache.maven.plugins</groupId>
  24.                 <artifactId>maven-compiler-plugin</artifactId>
  25.                 <configuration>
  26.                     <source>1.6</source>
  27.                     <target>1.6</target>
  28.                 </configuration>
  29.             </plugin>
  30.             <plugin>
  31.                 <groupId>org.apache.cxf</groupId>
  32.                 <artifactId>cxf-java2ws-plugin</artifactId>
  33.                 <version>${cxf.version}</version>
  34.                 <dependencies>
  35.                     <dependency>
  36.                         <groupId>org.apache.cxf</groupId>
  37.                         <artifactId>cxf-rt-frontend-jaxws</artifactId>
  38.                         <version>${cxf.version}</version>
  39.                     </dependency>
  40.                     <dependency>
  41.                         <groupId>org.apache.cxf</groupId>
  42.                         <artifactId>cxf-rt-frontend-simple</artifactId>
  43.                         <version>${cxf.version}</version>
  44.                     </dependency>
  45.                 </dependencies>
  46.                 <executions>
  47.                     <execution>
  48.                         <id>process-classes</id>
  49.                         <phase>process-classes</phase>
  50.                         <configuration>
  51.                             <className>com.unitedcoders.demo.PersonService</className>
  52.                             <genWsdl>true</genWsdl>
  53.                             <verbose>true</verbose>
  54.                         </configuration>
  55.                         <goals>
  56.                             <goal>java2ws</goal>
  57.                         </goals>
  58.                     </execution>
  59.                 </executions>
  60.             </plugin>
  61.         </plugins>
  62.     </build>
  63.    
  64.     <!-- Dependency definitions -->
  65.     <dependencies>
  66.    
  67.         <!-- Apache CXF dependencies -->
  68.         <dependency>
  69.             <groupId>org.apache.cxf</groupId>
  70.             <artifactId>cxf-rt-frontend-jaxws</artifactId>
  71.             <version>${cxf.version}</version>
  72.         </dependency>
  73.         <dependency>
  74.             <groupId>org.apache.cxf</groupId>
  75.             <artifactId>cxf-rt-transports-http</artifactId>
  76.             <version>${cxf.version}</version>
  77.         </dependency>
  78.        
  79.         <!-- Spring Dependencies -->
  80.         <dependency>
  81.             <groupId>org.springframework</groupId>
  82.             <artifactId>spring-core</artifactId>
  83.             <version>${spring-version}</version>
  84.         </dependency>
  85.         <dependency>
  86.             <groupId>org.springframework</groupId>
  87.             <artifactId>spring-web</artifactId>
  88.             <version>${spring-version}</version>
  89.         </dependency>
  90.        
  91.         <!-- Logging -->
  92.         <dependency>
  93.             <groupId>commons-logging</groupId>
  94.             <artifactId>commons-logging</artifactId>
  95.             <version>${commons-logging-version}</version>
  96.         </dependency>
  97.        
  98.         <!-- Testing -->
  99.         <dependency>
  100.             <groupId>junit</groupId>
  101.             <artifactId>junit</artifactId>
  102.             <version>${junit-version}</version>
  103.             <scope>test</scope>
  104.         </dependency>
  105.     </dependencies>
  106. </project>

My projects structure

  1. phil@invader:~/Projects/CXFExample$ tree
  2. .
  3. |-- README.txt
  4. |-- pom.xml
  5. `-- src
  6.     |-- main
  7.     |   |-- java
  8.     |   |   `-- com
  9.     |   |       `-- unitedcoders
  10.     |   |           `-- demo
  11.     |   |               |-- Person.java
  12.     |   |               |-- PersonService.java
  13.     |   |               `-- PersonServiceImpl.java
  14.     |   |-- resources
  15.     |   |   `-- application-context.xml
  16.     |   `-- webapp
  17.     |       `-- WEB-INF
  18.     |           `-- web.xml
  19.     `-- test
  20.         `-- java
  21.             `-- com
  22.                 `-- unitedcoders
  23.                     `-- demo
  24.                         |-- PersonServiceImplTest.java
  25.                         `-- PersonTest.java
  26.  
  27. 14 directories, 9 files
  28. phil@invader:~/Projects/CXFExample$

Let's first take a look on what has happened here. I defined some Apache CXF 2.2.6 dependencies which are needed to create my SOAP-webservice. To use the Spring framework in my little project I also added spring-core and spring-web in version 3.0.1 to my dependencies. Additionally I defined some other dependencies like JUnit and Apache commons-logging. But what has happend to the Maven 2 plugins? The first plugin I configured in my project is the maven-compiler-plugin. This plugin is a plugin of Mavens standard lifecycle. My configuration lets Maven 2 know which Java version should be used to compile my source. I prefer to use Java 1.6. The second plugin I defined is the cxf-java2ws-plugin. This Maven 2 plugin helps me to generate a WSDL-file from my annotated Java webservice-class.

Developing the SOAP webservice

To have a little showcase I created a simple class "Person" containing the only one member field "name". Later on the webservice should get a persons name from a client and answer with a short greeting like „Hello [NAME]!“.

  1. package com.unitedcoders.demo;
  2.  
  3. public class Person {
  4.  
  5.     private String name;
  6.    
  7.     public Person(String name) {
  8.         this.name = name;
  9.     }
  10.  
  11.     public String getName() {
  12.         return name;
  13.     }
  14.  
  15.     public void setName(String name) {
  16.         this.name = name;
  17.     }
  18.    
  19. }
After that I created a simple endpoint-interface for my webservice and annotated it as a @WebService.
  1. package com.unitedcoders.demo;
  2.  
  3. import javax.jws.WebService;
  4.  
  5. @WebService
  6. public interface PersonService {
  7.  
  8.     public String greetPerson(String name);
  9.  
  10. }
The implementation is as simple as the interface and is also annotated as a @WebService. Additionally the "endpointInterface" is defined as an annotation-parameter.
  1. package com.unitedcoders.demo;
  2.  
  3. import javax.jws.WebService;
  4.  
  5. @WebService(endpointInterface = "com.unitedcoders.demo.PersonService")
  6. public class PersonServiceImpl implements PersonService {
  7.  
  8.     public String greetPerson(String name) {
  9.         Person person = new Person(name);
  10.  
  11.         return "Hello " + person.getName() + "!";
  12.     }
  13.  
  14. }
That's all. These three little files are representing my whole webservice.

Combining everything

To get the webservice working I finally combined Spring and my webservice-implementation. Therefore I first defined the applications context. First I added three imports to my applications context. There are three imports that add CXF-specific bean definitions and configurations to my applications context. After that a singleton bean of my webservice is needed. And at least a jaxws:endpoint is defined that maps my bean to the endpoint to the path "/personService".
application-context.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="..."
  3.    xsi:schemaLocation="...">
  4.    
  5.     <import resource="classpath:META-INF/cxf/cxf.xml" />
  6.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  7.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  8.    
  9.     <!-- Spring manage ServiceBean -->
  10.     <bean id="personServ" class="com.unitedcoders.demo.PersonServiceImpl" />
  11.  
  12.     <!-- JAX-WS Service Endpoint -->    
  13.     <jaxws:endpoint id="personService" implementor="#personServ" address="/personService" />
  14.    
  15. </beans>
Now the context is ready to use. But what about the web.xml. The web.xml should now define a ContextLoaderListener to start up my Spring application context. Additionally a CXFServlet must be defined to make the application accessible as a webservice. I mapped to the CXFServlet to "/*". "/*" means that all services delivered by the CXFServlet are accessible right after the context (e.g. http://[HOST]:[PORT]/[CONTEXT]/[CXFSERVLET]).
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.    xsi:schemaLocation="...">
  4.  
  5.     <display-name>CXF Example Webservice</display-name>
  6.  
  7.     <listener>
  8.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  9.     </listener>
  10.  
  11.     <context-param>
  12.         <param-name>contextConfigLocation</param-name>
  13.         <param-value>classpath:application-context.xml</param-value>
  14.     </context-param>
  15.  
  16.     <servlet>
  17.         <servlet-name>CXFServlet</servlet-name>
  18.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  19.         <load-on-startup>1</load-on-startup>
  20.     </servlet>
  21.  
  22.     <servlet-mapping>
  23.         <servlet-name>CXFServlet</servlet-name>
  24.         <url-pattern>/*</url-pattern>
  25.     </servlet-mapping>
  26.    
  27. </web-app>

Testing the new SOAP-webservice

After building this Maven 2 project with „mvn clean package“ a new war-file will be placed in the target folder. This war-file now contains an entire webservice. If it is deployed on a Java web container like Apache Tomcat you can reach the webservice by accessing it in your browser.

http://[YOUR TOMCAT]:[PORT]/CXFExampleService/personService?wsdl

Webservice test in browser

To run some functional tests on the new webservice an test if the webservice works as expected I used soapUI which can be downloaded here. soapUI is a really famous tool that can be used to test webservices. It can be used as a standalone application or as a IDE plugin (e.g. for eclipse).

Webservice test in soapUI

Download the full example:

DOWNLOAD

Some resources:

http://cxf.apache.org
http://www.springsource.org/download
http://www.soapui.org

AttachmentSize
united-coders.com-CXFExample.tar_.bz22.53 KB
united-coders.com-CXFExample.zip8.26 KB

Comments

Anonymous's picture

Thanks for your guide!

Anonymous's picture

I have used your example to run my application by changing the parameters. I am getting the following error. Can anyone help me out?

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [application-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [application-context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:196)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
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.deployWAR(HostConfig.java:829)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:718)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1147)
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:578)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.io.FileNotFoundException: class path resource [application-context.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 36 more

Anonymous's picture

java.io.FileNotFoundException: class path resource [application-context.xml] cannot be opened because it does not exist

you sure this file is present?

Anonymous's picture

Applicationcontect.xml is available under the src/main/resources folder. Do I need to change the location of this ?

Nico Heid's picture

this is not the same as application-context.xml
change the file name.

Anonymous's picture

same file only. Mistake in mailing

Alisia Tweed's picture

I was looking out for information on this since quiet a long time now. I consider you’ve created some genuinely exciting points. It helps me very much to solve some problems. Its opportunity is so fantastic and working style so speedy. I think it may be help all of you. It could be a great tool in many ways for research. I will definitely use it in my work.. I have enjoyed reading, Thank you for your info, greatly appreciated.
Philadelphia Web Design

Anonymous's picture

good work, I appreciate it! detailed clean and easy to understand!

Andrew's picture

Can you give us a little more insight into the cxf files referenced within the application-context.xml?



I don't see those files in your tar file. What a they for and are they needed?

Phillip Steffensen's picture

The cxf files referenced in application-context.xml are delivered by the cxf dependencies.
You can take a closer look on them if you browse into these dependencies.

Christian Harms's picture
Anonymous's picture

Excellent work, just what I was looking for!-)

Anonymous's picture

hi,

Thanks you for your example. But i 'd like to ask you in which directory is generated the wsdl using the plugin cxf-java2ws-plugin.

Vincent's picture

Hi,

Excellent Job you did, it is very very useful to me.

Thanks,
Vincent

Rajesh Kollam's picture

Ecellent example. This is really helped me.

Vlad's picture

Excellent example, would be nice if wsdl had Person::name like in java rather than Person::arg0.
Also it offers manual testing using SOAP UI rather than having honest http junit test.

Vlad's picture

Here is the fix to make wsdl look better (person should have "name" attribute instead of "arg0"):
public String greetPerson(@WebParam(name = "name") String name);

Brian's picture

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.cxf.resource.ResourceManager' defined in class path resource [META-INF/cxf/cxf.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.unitedcoders.demo.PersonServiceImpl] for bean with name 'personServ' defined in class path resource [application-context.xml]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class com.unitedcoders.demo.PersonServiceImpl)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:196)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
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:546)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:905)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:740)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:592)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.unitedcoders.demo.PersonServiceImpl] for bean with name 'personServ' defined in class path resource [application-context.xml]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class com.unitedcoders.demo.PersonServiceImpl)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:570)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1277)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:382)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:376)
at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1109)
at org.apache.cxf.bus.spring.Jsr250BeanPostProcessor.postProcessAfterInitialization(Jsr250BeanPostProcessor.java:76)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:404)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
... 35 more
Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class com.unitedcoders.demo.PersonServiceImpl)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2737)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1124)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1612)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:258)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1229)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1200)
... 45 more
Sep 25, 2010 12:46:40 PM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Sep 25, 2010 12:46:40 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Sep 25, 2010 12:46:40 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()

Brian's picture

I found my problem, I was running tomcat under the wrong version of Java.

brian

Anonymous's picture

hey i am new to this soap spring webservices
can some one pls tell how can i use this "personService" as end point from my client application?

Thank you.

Jason's picture

I've been looking all over for examples on how to quickly get a webservice running under Spring 3.0. It's difficult to find any JAX-WS examples. I tried the jax-ws-commons extension and could not get it to work. After spending most of my day trying to get something to work, I had Apache CXF up and running in about an hour. Your example made it a piece of cake. Thanks a lot for putting up this page!

Anonymous's picture

Finally something that worked! And so simple. I too tried to get Spring JAX-WS working first but it was overcompilacted disaster. Plus this works on a mere servlet containers, no need to involve Glassfish. @WebParam was a good hint too.

Anonymous's picture

I generated some web service tests using soapUI and added them to maven build. Details are found at http://www.soapui.org/Test-Automation/maven-2x.html. However all of the pom.xml tags on that page are missing since somebody added xml straight into html... However you can see the tags using browsers view source.

Here is quick start.. First use soapui to generate test from WSDL then save the project to maven project tree under src/test/soapui/.

Then add soapui plugin stuff to pom.xml.
1. Add eviware maven repo:

  1. <pluginRepositories>
  2.         <pluginRepository>
  3.                 <id>eviwarePluginRepository</id>
  4.                 <url>http://www.eviware.com/repository/maven2/</url>
  5.         </pluginRepository>
  6. </pluginRepositories>

2. Add soapui plugin to plugins section:

  1. <plugin>
  2.         <groupId>eviware</groupId>
  3.         <artifactId>maven-soapui-plugin</artifactId>
  4.         <version>3.6.1</version>
  5.  
  6.         <configuration>
  7.                 <!-- path to soapui test project -->
  8.                 <projectFile>src/test/soapui/productService-soapui-project.xml</projectFile>
  9.                 <outputFolder>src/test/soapui/output/</outputFolder>
  10.         </configuration>
  11. </plugin>

3. Run tests with mvn eviware:maven-soapui-plugin:test

I first tried to add web service test execution to maven test phase, but that didn't work out too well since application was not yet deployed and jetty was not yet running :p

Anonymous's picture

I am getting this error when I am trying to run the sample application in Jetty.

Problem accessing /personService. Reason:

loader constraint violation: when resolving overridden method "org.apache.cxf.jaxb.attachment.JAXBAttachmentUnmarshaller.getAttachmentAsDataHandler(Ljava/lang/String;)Ljavax/activation/DataHandler;" the class loader (instance of org/mortbay/jetty/webapp/WebAppClassLoader) of the current class, org/apache/cxf/jaxb/attachment/JAXBAttachmentUnmarshaller, and its superclass loader (instance of <bootloader>), have different Class objects for the type javax/activation/DataHandler used in the signature

Caused by:

java.lang.LinkageError: loader constraint violation: when resolving overridden method "org.apache.cxf.jaxb.attachment.JAXBAttachmentUnmarshaller.getAttachmentAsDataHandler(Ljava/lang/String;)Ljavax/activation/DataHandler;" the class loader (instance of org/mortbay/jetty/webapp/WebAppClassLoader) of the current class, org/apache/cxf/jaxb/attachment/JAXBAttachmentUnmarshaller, and its superclass loader (instance of <bootloader>), have different Class objects for the type javax/activation/DataHandler used in the signature
	at org.apache.cxf.jaxb.JAXBDataBase.getAttachmentUnmarshaller(JAXBDataBase.java:78)
	at org.apache.cxf.jaxb.io.DataReaderImpl.createUnmarshaller(DataReaderImpl.java:92)
	at org.apache.cxf.jaxb.io.DataReaderImpl.read(DataReaderImpl.java:128)
	at org.apache.cxf.interceptor.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:106)
	at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
	at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:109)
	at org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:98)
	at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:406)
	at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:178)
	at org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:142)
	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:103)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)
	at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
	at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
	at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
	at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
	at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
	at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
	at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
	at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
	at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
	at org.mortbay.jetty.Server.handle(Server.java:326)
	at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
	at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:939)
	at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
	at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
	at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
	at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
	at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
rakesh kumar's picture

org.apache.cxf.interceptor.Fault: Response was of unexpected text/html ContentType. Incoming portion of HTML stream: CXF - Service listAvailable SOAP services:

PersonService
  • greetPerson
Endpoint address: http://localhost:8080/SpringCXF2/personService
WSDL : {http://demo.unitedcoders.com/}PersonServiceImplService
Target namespace: http://demo.unitedcoders.com/







at org.apache.cxf.interceptor.StaxInInterceptor.handleMessage(StaxInInterceptor.java:79)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:247)
at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:733)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:2201)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:2071)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1925)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:662)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:247)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:516)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:313)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:265)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:124)
at $Proxy31.greetPerson(Unknown Source)
at com.unitedcoders.demo.TestClient.main(TestClient.java:15)
Exception in

Prasanna's picture

Thank you so much.. i struggled for almost a day.. but couldn't get it right.. but your tutorial.. got the juices flowing in under 30 mins.

it is working's picture

I was thinking this was impossible me to make, but finally found this tutorial and understood it in a minutes.

Thank you very much!

Anonymous's picture

Caused By: java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
Truncated. see log file for complete stacktrace

sfeher's picture

It might be filtered out by maven war plugin and/or file name has mixed case.

Anonymous's picture

Tried using winzip to unpack and view these files - no luck. Spent too much time... moving on.

Anonymous's picture

if you use winzip 11 or higher it will work. or use 7zip.

but you don't want to tell me that you do not know the file extension bz2 or could at least google it?

Anonymous's picture

My Winzip 14.5 unzipped the bz2 into a tar file, but from there, I could find nothing about turning it into something useful in Windows 7. Yes, I googled the $&*# out of it, and spent too much time on it, but thanks for trying to help me out.

Nico Heid's picture

hey, i repackaged it into a zip for you (see attachements): http://united-coders.com/sites/default/files/united-coders.com-CXFExampl...

you'd either need 7zip as mentioned, or if you end up wit a tar and don't want 7zip, you could use tar for windows.

but now you can just use the zip file.

hope we could help

yerico's picture

Use Total Commander! Select the .bz2 file, press CTRL + PAGE DOWN. You will get the .tar file. Repeat the pressing of the CTRL + PAGE DOWN and you will see the source you need. Simple copy it to the location you want.

Evgeny Sirikh's picture

lol, you such a funny guys. sure no cfx-...xml in this archive. No archiver will help find files which not exist in archive :)
Those files located in cxf-2.x.x.jar. Make sure you have it in class path.

Regards.

Addy's picture

Hi,

I am new to Soap-web service using Spring3.x and Apache CXF2.x

plus i have a security restriction on my workstation that i can not go online to get the dependencies using maven. Can you please help me how can i develop spring web service, generate the wsdl file and test it with soapUI after deploying it on IBM Websphere 7 without involving maven.

i will be really thankful.

Addy

QWEB's picture

Hi,

Very good article on CXF with Spring. Clean explanation.

It was really helpful

Thanks,
QWEB

deviprasad's picture

Hai...
This is deviprasad Raju, i'm new to this SOAPUI and i got stucked with the debugging of my web application using xml's. i was able debug my application but when i run run request xml by passing some inputs in the response xml i'm getting error name "error in creating instance of the class" what it actually means... can you please tell how to get out from this.............

yerico's picture

Great job, it was very helpful for me. Thanks!

sam's picture

can u pl provide ant version of the same webservice

top's picture

i am using spring MVC , i have dispatcher-servlet.xml file , so i can't write on that file because i am doing mapping in controllers (using @Controller
@RequestMapping("/home") ). in this case how can i do the mapping part for above web services ?

thanks

kiran's picture

Phillip,
Thank you very much for an excellent tutorial.
I have this question.
while testing the service we give the url as
http://localhost:8080/CXFExampleService/personService
in the SoapUI.

My question is how and where the string "personService" is mapped to the service name in the wsdl ?

-Kiran

kiran's picture

Phillip,

I was able to figure the answer to my question. The Application-cotext.xml explains very well to my question.

Thanks any way.

Anonymous's picture

Hi

Its very good article .. . Now i have one doubt that ,how we can I create a client application (J2EE application ) to communicate with this server web services.
Please give me any suggestion

Anonymous's picture

Hi,
I just found error in application-context file: " Multiple annotations found at this line:
- schema_reference.4: Failed to read schema document 'http://cxf.apache.org/schemas/jaxws.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 'jaxws:endpoint'."

Please help me fix this one.

Thanks so much.

Sumit Bansa;l's picture

Hi, many thanks for this simple & crisp example, it was really helpful.

Regards,
sSumit Bansal

Alok Chandra's picture

Hi,

Many thanks for this examples. But use the following version of spring while running this example
3.0.2.RELEASE.

Other wise it will give the following exception
java.lang.ClassNotFoundException: org.springframework.core.convert.support.PropertyTypeDescriptor

Arun Jolly's picture

Awesome...I just changed the source and target to 1.5 ( cos i wanted it that way ) in the pom, built it, deployed and tested it...All in 5 minutes !!...Awesome Post Man !!...Thanks !!

KP's picture

Excellent Tutorial, Easy and effective. Thanks for sharing.

KP's picture

Excellent Tutorial, Easy and effective. Thanks for sharing.