How to create a WSDL-first SOAP client in Java with CXF and Maven

Posted on October 19, 2008. Filed under: Java Programming |

This article was originally written and posted in October 2008. However, I’ve retired this blog. Because of that, as of December 2011 I’ve copied the complete text of the original article over to my new web site, just click here to read it. As of December 2011 the code was tested and still works correctly. Note I’m an iOS developer nowadays, not a Java developer, so regretfully I can’t offer any support nor updates if it doesn’t work for you. Please feel free to read, comment, and link to the article at its new home.

Looking for just the source code? It’s available in zipped format, just click here to download it from my new web site.

About these ads

30 Responses to “How to create a WSDL-first SOAP client in Java with CXF and Maven”

RSS Feed for Logic Sector Blog Comments RSS Feed

Interesting, and very complete. Thanks for sharing it.

EXCELLENT! This post has been very useful for me.

Thanks

Thanks… I have been looking for a complete client “Getting Started” for CXF. Eventually I wold have been stuck with axis forever. When the list of silly people (“cxf.xml”) is sufficiently long on should consider pointing out the fact that it’s needed more clearly. I believe your work should be added to the documentation part of cxf as it lowers the entry level considerably.
Thanks, again.

This is what I was working on and was looking for this, a complete working example. Just one question, how about if we are behind a proxy server:- how do we set the proxy setting in the client to get through the proxy/firewall.

I tried it just now, I get the following stack trace:
C:\Documents and Settings\irshad.KSU1\Desktop\weather-client\weather-client>java -cp target/weather-client-jar-with-dependencies.jar
[15:45:14,890 DEBUG SoapClient]: Creating weather service instance (Note: Weather = Service subclass)…
[15:45:37,593 WARN ControlledValidationXmlBeanDefinitionReader]: Ignored XML validation warning
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document ‘http://www.springframework.org/schema/beans/sprin
document; 2) the document could not be read; 3) the root element of the document is not .
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
(snip)
Dec 16, 2008 3:45:37 PM org.apache.cxf.bus.spring.SpringBusFactory createBus
WARNING: Failed to create application context.
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 9 in XML document from class path resource [cxf.xml] is
eException: cvc-elt.1: Cannot find the declaration of element ‘beans’.
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘beans’.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
(snip)
[15:45:37,687 ERROR SoapClient]: An exception occurred, exiting
java.lang.RuntimeException: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 9 in XML document from class
ption is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘beans’.
at org.apache.cxf.bus.spring.SpringBusFactory.createBus(SpringBusFactory.java:97)
at org.apache.cxf.bus.spring.SpringBusFactory.createBus(SpringBusFactory.java:87)

Hi Irshad, from the exception trace I’m guessing you have an error in your cxf.xml file. You can download the complete source in a Zip file using a link near the end of my original post. Regarding your proxy/firewall issue–I don’t know. I haven’t had that issue, hopefully Google can help, or try the official CXF mailing list (the mailing list also has a searchable archive, which you might want to check first before sending email to the CXF list). Good luck!

Thanks for the reply. I downloaded the code from the link provided by you, but it seams that the cxf.xml file provided in the download has the problem and that is why I am hitting this stack trace.

Hi Irshad, I just tried downloading the linked source to my work PC, and it built and ran fine there too. My suggestions are (1) download the source again, (2) do not open the cxf.xml file nor any other files in a text editor, just in case it’s auto-saving them and corrupting them somehow, (3) build it from clean, and (4) make sure you have Java 1.5 etc installed and set as your default JVM.

Remember, you can build the code from clean with mvn clean assembly:assembly install and run it with java -cp target/weather-client-jar-with-dependencies.jar com.logicsector.soapclient.SoapClient once you’ve built it. Good luck with it.

OK, after a bit of struggle I solved the problem by putting the following in the pom.xml:
org.springframework
spring
2.5.5

Thanks for your effort. One thing that I have noticed is that log4j.xml and cxf.xml file gets added twice to the weather-client-jar-with-dependencies.jar. You will find 2 copies of cxf.xml and log4j.xml file in this jar file.

Thanks for your help. I tried it a fresh again today as you have mentioned and this is what I get:

C:\weather-client>java -cp target/weather-client-jar-with-dependencies.jar com.logicsector.soapclient.SoapClient
[12:17:06,500 DEBUG SoapClient]: Creating weather service instance (Note: Weather = Service subclass)…
[12:17:32,046 WARN ControlledValidationXmlBeanDefinitionReader]: Ignored XML validation warning
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document ‘http://www.springframework.org/schema/beans/spring-beans.xsd’, because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .
(snip)
WARNING: Failed to create application context.
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 6 in XML document from class path resource [cxf.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘beans’.
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘beans’.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
(rest of stack trace snipped for length)

Hey, THANKS so much for sharing this!

I can get it work, except for the fact that the forecast info that is returned is wrong.

Here is what i get:
===================

C:\weather-client>java -cp target/weather-client-jar-with-dependencies.jar com.logicsector.soapclient.SoapClient
[20:31:38,140 DEBUG SoapClient]: Creating weather service instance (Note: Weather = Service subclass)…
[20:31:39,685 DEBUG SoapClient]: …Done! weatherService instance: com.cdyne.ws.weatherws.Weather@9bad5a
[20:31:39,685 DEBUG SoapClient]: Time required to initialize weather service interface: 1.545 seconds
[20:31:39,701 DEBUG SoapClient]: weatherSoap instance: org.apache.cxf.jaxws.JaxWsClientProxy@1250ff2
[20:31:40,122 DEBUG SoapClient]: Time required to invoke ‘getCityForecastByZIP': 0.421 seconds
[20:31:40,122 DEBUG SoapClient]: forecastReturn: com.cdyne.ws.weatherws.ForecastReturn@fa5ff3
[20:31:40,122 DEBUG SoapClient]: forecastReturn city: Menlo Park
[20:31:40,122 DEBUG SoapClient]: forecastReturn state: CA
[20:31:40,122 DEBUG SoapClient]: forecastReturn result: com.cdyne.ws.weatherws.ArrayOfForecast@1b17d49
[20:31:40,122 DEBUG SoapClient]: forecastReturn response text: City Found
[20:31:40,122 DEBUG SoapClient]:
[20:31:40,122 DEBUG SoapClient]: forecast date: Monday, January 5 2009
[20:31:40,122 DEBUG SoapClient]: forecast description: Mostly Cloudy
[20:31:40,122 DEBUG SoapClient]: forecast temperature high: 60
[20:31:40,122 DEBUG SoapClient]: forecast temperature low: 45
[20:31:40,122 DEBUG SoapClient]: forecast precipitation day: %
[20:31:40,122 DEBUG SoapClient]: forecast precipitation night: %
[remainder deleted by blog owner]

You’re welcome Kay. Glad you found it useful!

Regarding your problem with the returned precipitation info being wrong…I believe that’s probably a problem at the source, i.e., the service provider is sending the wrong information. The fact that you’re getting the correct city for the zip code, correct forecast description, temperature high/low, etc., means that you’re successfully connecting to the service via SOAP.

[...] client with Apache CXF http://logicsector.wordpress.com/2008/10/19/how-to-create-a-wsdl-first-soap-client-in-java-with-cxf-… help | terms of service | privacy | report a bug | flag as objectionable Hosted [...]

Excellent article which lightens me quickly to do an app
I have the same error as mentioned by Irshad. As per his say,i added the Spring jar in my client application and it works well in a standalone java client. I want the same thing in a servlet. While i implemented the same code in a servlet, i get this error. Anyone can help me??

java.lang.IncompatibleClassChangeError
org.apache.cxf.wsdl11.WSDLServiceBuilder.copyExtensionAttributes(WSDLServiceBuilder.java:133)
org.apache.cxf.wsdl11.WSDLServiceBuilder.buildServices(WSDLServiceBuilder.java:273)
org.apache.cxf.wsdl11.WSDLServiceBuilder.buildServices(WSDLServiceBuilder.java:184)
org.apache.cxf.wsdl11.WSDLServiceFactory.create(WSDLServiceFactory.java:129)
org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromWSDL(ReflectionServiceFactoryBean.java:325)
org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:429)
org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:191)

any help?

Hi Varun, please see the info at this link, it looks similar to your problem. I haven’t seen the problem myself, so YMMV. (You could always try the CXF users mailing list if you still can’t figure out the problem, good luck.)

http://markmail.org/message/47bqpoaxopludlks#query:java.lang.IncompatibleClassChangeError%20WSDLServiceBuilder.copyExtensionAttributes+page:1+mid:hh7hw5wodeaujph3+state:results

Thank you for your reply. I already found the link which u sent and solved the problem.

You are simply superb. I was fighting with >> org.apache.cxf.BusException: No binding factory for namespace http://schemas.xmlsoap.org/soap/ registered >>. Good that I found your link. You Rock!!!!!

thanks alot…..

Excellent and Great. I nver have seen a complete article like this and the instructions worked 100% to my surprise.

Wish the author write more and more such articles.

Thaks direct from my heart.

Hi,It is good article.I’m getting below exception when i run this application on eclipse and i’m using java 1.5.
Can any one please help me regarding this….

java.util.zip.ZipException: The system cannot find the path specified
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.(ZipFile.java:203)
at java.util.jar.JarFile.(JarFile.java:132)
at java.util.jar.JarFile.(JarFile.java:70)
at org.apache.cxf.maven_plugin.CodegenUtils.getClassTime(CodegenUtils.java:74)
at org.apache.cxf.maven_plugin.CodegenUtils.getCodegenTimestamp(CodegenUtils.java:48)
at org.apache.cxf.maven_plugin.WSDL2JavaMojo.execute(WSDL2JavaMojo.java:221)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:579)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:498)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmentForProject(DefaultLifecycleExecutor.java:265)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:191)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:149)
at org.apache.maven.DefaultMaven.execute_aroundBody0(DefaultMaven.java:223)
at org.apache.maven.DefaultMaven.execute_aroundBody1$advice(DefaultMaven.java:304)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:1)
at org.apache.maven.embedder.MavenEmbedder.execute_aroundBody2(MavenEmbedder.java:904)
at org.apache.maven.embedder.MavenEmbedder.execute_aroundBody3$advice(MavenEmbedder.java:304)
at org.apache.maven.embedder.MavenEmbedder.execute(MavenEmbedder.java:1)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:176)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:63)
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:585)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:408)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:351)
at org.codehaus.classworlds.Launcher.main(Launcher.java:31)

WoW!!! Excellent cxf client side post. I had some days for “org.apache.cxf.BusException: No binding factory for namespace http://schemas.xmlsoap.org/soap/ registered”. I solved it!!! Thanks a lot!

Great work ! Thank you very much !

One of the best document i have ever read thanks a lot.Appreciate all your efforts

Very nice article!

the cxf.xml is not neccessary,we can just use spring contxt
context.xml. thanks!

Explicit promotion ;)

For desperate Java developpers that have to request SOAP web-services, there is a new API available in a early version : http://soap-dust.sourceforge.net/

It fits in one jar.

It does not require to generate a bunch of Java code before requesting the web-service.

Until SOAP is definitely deprecated, this may be helpfull.

Excellent post. It helped me a lot

thanks a lot for the cxf.xml fix – i ran into the same and googled…

Great work and thanks for sharing. However, I would like to add here what happened in my case:

If I just add xerces dependency, I started getting NullPointerException. To avoid that I have exclude the xerces in “cxf-rt-frontend-jaxws” and “cxf-rt-transports-http” dependencies.

xerces
xercesImpl


Comments are closed.

Liked it here?
Why not try sites on the blogroll...

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: