I have a multi-module project using Maven. On one of the modules I have several web services developed using Apache CXF Framework 2.5.4. At the moment I have two “problems” or questions.
First of all, if I call to a method of one of the web services that should return a List, if the list is empty, it returns “null” instead of the empty list.
I was trying to find out what could be the problem, if it’s a bug of the CXF version I’m using or if I should use some annotation to modify the definition of the method or the response, but I couldn’t find anything. I’ve seen some people with the same problem, but no solution.
The other thing I wanted to ask is: I’m developing a web application using MVC pattern. I’m wondering which way I should call the web service from the Controller instead of using ClasspathXmlCpplicationContext and then context.getBean().
For example, the bean definition for one of the web services on the client side is:
<jaxws:client id="deviceWSClient"
serviceClass="..IDeviceWebService"
address="http://localhost:8080/../DeviceWS" />
I’ve already tried usin @Autowired or @WebServiceRef annotations. With these it works but not doing a HTTP request to the web service, I guess it gets the dependency from the local repository. I think what I need is the way of injecting this bean on the Controller.
To answer your questions
For your first question: If the list is empty it is correctly handled by CXF version 2.6.1 – the service returns a empty. Just to demonstrate I have a sample service where types are defined this way:
If I return a empty memberDetails above, the xml that goes over the wire is this:
EDIT
It is correctly handled as part of a wrapper type like above, but DOES return null if instead of returning a wrapper type, the list is directly returned.
Consider a Webservice interface defined this way:
If the List returned is an Empty list, it gets serialized as null by CXF 2.6.1 also.
The workaround is to use a wrapper type
EDIT END
For your second question:
You are creating a client bean this way:
Once you have created a Spring bean this way, you can treat it just like a normal Spring bean and inject it the way you would do with any normal Spring bean, for eg, either inject it this way:
or use
@AutowiredOr user
@ResourceThese are the usual ways of injecting in a bean.
I have a sample application at this github location that you can play with:
https://github.com/bijukunjummen/memberservice-codefirst.git
Just start up the server using
mvn tomcat:runand run a testorg.bk.memberservice.TestCxfIntegrationTestwhich will make a request to the CXF service.