How to use Apache CXF as client?
Asked Answered
S

1

6

I know how to generate client stubs using Apache CXF. However, when I try to run the generated classes, it uses JAXWS. Also, I notice that the import classes of the generated classes are from the javax package. How can I use set the generated classes to use the Apache CXF libraries instead of the JAXWS libraries?

Below is the code I use to generate the client stubs:

wsdl2java -frontend jaxws21 -wsdlLocation "META-INF/wsdl/WSCustom.wsdl" -client -d C:\Workspace\WSClient\META-INF\wsdl\ "C:\Workspace\WSClient\META-INF\wsdl\WSCustom.wsdl"
Sciential answered 4/3, 2013 at 8:11 Comment(0)
H
6

The stubs are correct, there shouldn't be any CXF-specific imports in them because all the information CXF needs can be represented using the JAX-WS standard annotations. At runtime the CXF client libraries will be used if they are on the class path, or the RI ones built in to the JDK will be used if CXF is not available. The generated stubs will work with either.

You asked in the comments about which CXF JARs are required if you're just running a client - as far as I know it's just cxf-rt-frontend-jaxws and cxf-rt-transports-http plus their transitive dependencies. If your project is built with maven then just declare those two dependencies and everything else should come in automatically, if not then download the Apache Ivy main JAR and then run

java -jar ivy-2.3.0.jar -dependency org.apache.cxf cxf-rt-frontend-jaxws 2.7.3 -retrieve "[artifact]-[revision](-[classifier]).jar"
java -jar ivy-2.3.0.jar -dependency org.apache.cxf cxf-rt-transports-http 2.7.3 -retrieve "[artifact]-[revision](-[classifier]).jar"

This should resolve the transitive dependencies and download the relevant JARs from Maven Central into the current directory.

Hilde answered 4/3, 2013 at 8:20 Comment(8)
Hi, @Ian. Thanks for your reply. If both CXF and the build-in JDK are available on the classpath, which will be the priority? Also, is there a way not to put it in the classpath and just include it only on the lib folder of the application? How can I check which if its already using the CXF lib?Sciential
@Sciential when I say "on the classpath" I don't necessarily mean you have to set an environment variable, I just mean the jars are available to your app in whatever way it needs (WEB-INF/lib if it's a web application, java -cp for a command line app, whatever). It'll use whichever is found first - if you have both the CXF and Metro jars "on the classpath" then it'll be whichever is listed first, if CXF is in your app and you're relying on the built in copy of Metro in rt.jar then CXF should win.Hilde
In terms of checking which is in use at a given time you can crank up the logging level (CXF docs, ignore the bit at the top about interceptors, that's only if you want to log the raw soap messages that are sent and received).Hilde
Thanks again for the explanation! I get it now. I thought you were referring to the environment variable.Sciential
Just one more question. Which CXF jars do I need to copy in my lib folder with the assumption that I only need to use CXF to run the client stubs? I check the CXF jars and they are too many.Sciential
@Sciential I've added a trick you can use with Ivy to resolve the transitive dependencies.Hilde
Thanks, @Ian! I don't use Maven but Apache Ivy would be handy. Thanks again! :DSciential
Thanks @IanRoberts . Do you know how to ignore the "optional" maven artifacts when fetching with ivy? I don't need the spring stuff. I couldn't find anything here: ant.apache.org/ivy/history/latest-milestone/standalone.html. Probably somewhere hidden in settings or configuration?Caviar

© 2022 - 2024 — McMap. All rights reserved.