Sunday, November 23, 2008

Consuming web services with Spring

Consuming web services with Spring framework is amazingly easy. It avoids the need of creating client side stubs during compile time and does the same at run time. A typical web service client can be configured as shown below-

<bean id="myWebService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
    <property name="serviceInterface" value="pkg.MyService"/>
    <property name="wsdlDocumentUrl" value="http://localhost:8080/testws/myService?wsdl"/>
    <property name="namespaceUri" value="http://com.pkg/"/>
    <property name="serviceName" value="MyService"/>
    <property name="portName" value="MyServicePort"/>
Where serviceInterface is the business interface that clients will use for invoking service methods. wsdlDocumentUrl is the URL for the published WSDL file. Spring will download WSDL from the given URL and generate the client side stubs when it creates the bean usually during application start up. namespaceUri corresponds to the targetNamespace in the .wsdl file. serviceName corresponds to the service name in the .wsdl file. portName corresponds to the port name in the .wsdl file.

Now accessing web service pretty easy, just get the bean from Spring context and use that to invoke methods-
MyService myService = springContext.getBean("myWebService");
myService.someMethod(. . .);
One downside of this approach is that the web service must be up running when consumer application is being deployed. To avoid this problem one can use lazy-init="true" to lazily initialize the beans when they are first requested but all this quickly becomes unmanageable when we have an array of SOA based applications. In such a scenario one have to strictly follow the order of application deployment else deployment might fail.


Anonymous said...

Hello Vinod,
I tried using this but no success yet.

I am getting an exception saying:
org.springframework.remoting.jaxws.JaxWsSoapFaultException: System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: .

Can you please let me know how do I solve this exception?

I am using the example service from


Thanks & Regards

Anonymous said...


If you can show the code, then I may have a look at that.

blackmoon said...

hi vinod ,

i am using the same but getting the Qname not null erorr while creating the Bean.
can you please suggest.

Anonymous said...

Hi Vinod,

I want to ask silly question :D, what is it mean by serviceInterface property, I mean what should I put as the value in there, is it the interface in the web service server or we should create another class in the client as the value?


Anonymous said...

Btw, I wonder with the packaging also...


Anonymous said...

serviceInterface is the interface, based on that JaxWsPortProxyFactoryBean will create a proxy. You need to create it to consume the web service. See here for more details.

Anonymous said...

Owh I think Im missing one step here...CMIIW i have to build jar of my web service first? import is as library in the client, then the serviceInterface will point to that jar, is that correct? I think that spring reference, must be read throughly...T.T

Anonymous said...

Hi Vinod,
I think I am missing something here, the question is also about the Service interface. I am supposed to consume .Net service. I cannot bundle the interface in a jar. Do I need to redefine the C# interface on the Client?
Spring Doc is too vague at least for me. Your help is really appreciated. thanks

Anonymous said...

You need to create that interface and related POJOs manually to consume the service through Spring.

Instead of using Spring, I prefer to consume a web service by creating client side stubs. See step #6 and 7 in Building JAX-WS web service.

Anonymous said...

Hi Vinod,

I did the similar example explained above but i stuck with below error.

Caused by: org.xml.sax.SAXException: Deserializing parameter 'Output': could not find deserializer for type {}Output
at org.apache.axis.message.RPCHandler.onStartChild(
at org.apache.axis.encoding.DeserializationContext.startElement(

Any idea how to keep deserializer for Output in Spring based WS call?


Anonymous said...


How to use this example when being behind the HTTP proxy?


Anonymous said...


Proxy authentication in Java may be useful to you.

Maciej said...


I tried this but it doesn't work for me when trying to call a web service method. I get:

org.apache.cxf.interceptor.Fault: Could not send Message.

followed by

Caused by: cannot retry due to proxy authentication, in streaming mode
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(
at org.apache.cxf.transport.AbstractConduit.close(
at org.apache.cxf.transport.http.HTTPConduit.close(
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(

Any ideas?


kaushal bhati said...

nice....i have done example and running fine...great.............

Laxmipathi said...

Hi Kosal

If possible can you please share your code, even i tried this example with but its not working.

kaushal bhati said...

ya sure laxmipathi...

Anonymous said...

Hi Vinod,

As per your guidelines i was trying to consume the web service and i am getting Null pointer Exception. Please have a look

SEVERE: Exception sending context initialized event to listener instance of class de.hybris.platform.spring.HybrisContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'integrationTesting' defined in ServletContext resource [/WEB-IN
Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.web.context.ContextLoader.createWebApplicationContext(
at org.springframework.web.context.ContextLoader.initWebApplicationContext(
at org.springframework.web.context.ContextLoaderListener.contextInitialized(
at org.apache.catalina.core.StandardContext.listenerStart(

Anonymous said...

can pls post the "pkg.MyService" file also

Anonymous said...

That you need to create. This is the service definition interface created based on WSDL.

semaj said...

The lazy-init="true" didn't work for me. I saved a local copy of the wsdl file and modified the wsdlDocumentUrl and pointed to the local copy. Then, I added .

The endpointAddress overrided the one specified in the local wsdl file.

If I don't do this, my local dev server won't start when the remote dev server is not online.

semaj said...

The blogger block the code snippet I wrote.
This is what I added.
property endPointAddress value=wsdl location

Hope this will help for someone.


bhaus said...

Hi Vinod,

I have tried to implement WS using the Spring but getting the error.

Error is Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emailValidateService' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is class: net.webservicex.IsValidEmail could not be found

Here is my code.

@WebService(name = "ValidateEmailSoap", targetNamespace = "")

public interface EmailValidateService {

* This method returns true or false for a given email address
* @param email
* @return
* returns boolean

@WebMethod(operationName = "IsValidEmail", action = "")
@WebResult(name = "IsValidEmailResult", targetNamespace = "")
@RequestWrapper(localName = "IsValidEmail", targetNamespace = "", className = "net.webservicex.IsValidEmail")
@ResponseWrapper(localName = "IsValidEmailResponse", targetNamespace = "", className = "net.webservicex.IsValidEmailResponse")
public boolean isValidEmail(
@WebParam(name = "Email", targetNamespace = "")
String email);



public class Test {
private static ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
private static EmailValidateService service = (EmailValidateService) context.getBean("emailValidateService");

public static void main(String[] args) {
}catch(Exception e) {


Anonymous said...


The exception is very clear from the error message "net.webservicex.IsValidEmail could not be found". Do you have this class in your classpath ?

bhaus said...

No I dont have that class
in my classpath.
I'm not accessing my local WS. I'm accessing the Third party WSDL.


Here is my ApplicationContext.xml content

bhaus said...

sorry dont know why applicationcontext.xml content is not displaying
I'm pasting again.

Next Generation JEE said...


I need to consume the webservice using spring,as per urs i tried and got the Excpetion: A WebService annotation is not present on class

do we need to create stubs manually?

kosal, can u send me a executed code to my mail.


New Mexico Service Annabelle said...

Hi, Kosal!

Please send me your code too. Tried this and it's not working properly yet. Would appreciate the help.

annabelle [at] ardham [dot] com

Thank you!

HarpreetSingh said...

Hi Vinod

I am trying to intercept the request and response SOAP messages with the configuration you have specified.

Can you please tell me on how to do this?


Vinod Singh said...

@Harpeet, What do you meant by interception? If you want to see the SOAP messages being interchanged between client and server then try Soap / http Monitoer. SoapUI is also a good option. In case you want to do some operations even before service is invoked then SOAP handler are the way to go.

HarpreetSingh said...

I need to log the SOAP request and response in the Log file. I am able to do this for the services I am creating using the SoapEnvelopeLoggingInterceptor however this works only for the service endpoints created with @Endpoint and @PayloadRoot annotations.

I have a third party wsdl which I am consuming as per the method stated by you and need to log the request and response in Log file. Please help.

Samuel Sosa said...

Hi Vinod,

Im having this exeption when trying to consume a WebMethod: Client received SOAP Fault from server: Cannot find dispatch method for .....

Is like is not getting the methods from my wsdl.

Any suggestion?