Configuring a Spring Client (Option 1)
The easiest way to add a Web Services client to a Spring context is to use the <jaxws:client> element (similar to the <jaxws:endpoint> element used for the server side). Here's a simple example:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="helloClient"
serviceClass="demo.spring.HelloWorld"
address="http://localhost:9002/HelloWorld"/>
</beans>
The attributes available on <jaxws:client> include:
Name |
Type |
Description |
---|---|---|
id |
String |
A unique identified for the client, which is how other beans in the context will reference it |
address |
URL |
The URL to connect to in order to invoke the service |
serviceClass |
Class |
The fully-qualified name of the interface that the bean should implement (typically, same as the service interface used on the server side) |
serviceName |
QName |
The name of the service to invoke, if this address/WSDL hosts several. It maps to the wsdl:service@name. In the format of "ns:SERVICE_NAME" where ns is a namespace prefix valid at this scope. |
endpointName |
QName |
The name of the endpoint to invoke, if this address/WSDL hosts several. It maps to the wsdl:port@name. In the format of "ns:ENDPOINT_NAME" where ns is a namespace prefix valid at this scope. |
bindingId |
URI |
The URI, or ID, of the message binding for the endpoint to use. For SOAP the binding URI(ID) is specified by the JAX-WS specification. For other message bindings the URI is the namespace of the WSDL extensions used to specify the binding. |
bus |
Bean Reference |
The bus name that will be used in the jaxws endpoint (defaults to cxf ). |
username |
String |
|
password |
String |
|
wsdlLocation |
URL |
A URL to connect to in order to retrieve the WSDL for the service. This is not required. |
createdFromAPI |
boolean |
This indicates that the client bean was already created using jaxws API's thus at runtime when parsing the bean spring can use these values rather than the default ones. It's important that when this is true, the "name" of the bean is set to the port name of the endpoint being created in the form "{http://service.target.namespace} PortName". |
It also supports many child elements:
Name |
Description |
---|---|
jaxws:inInterceptors |
The incoming interceptors for this endpoint. A list of <bean> or <ref> elements. Each should implement org.apache.cxf.interceptor.Interceptor or org.apache.cxf.phase.PhaseInterceptor |
jaxws:inFaultInterceptors |
The incoming fault interceptors for this endpoint. A list of <bean> or <ref> elements. Each should implement org.apache.cxf.interceptor.Interceptor or org.apache.cxf.phase.PhaseInterceptor |
jaxws:outInterceptors |
The outgoing interceptors for this endpoint. A list of <bean> or <ref> elements. Each should implement org.apache.cxf.interceptor.Interceptor or org.apache.cxf.phase.PhaseInterceptor |
jaxws:outFaultInterceptors |
The outgoing fault interceptors for this endpoint. A list of <bean> or <ref> elements. Each should implement org.apache.cxf.interceptor.Interceptor or org.apache.cxf.phase.PhaseInterceptor |
jaxws:features |
The features that hold the interceptors for this endpoint. A list of <bean> or <ref> elements |
jaxws:handlers |
The JAX-WS handlers for this endpoint. A list of <bean> or <ref> elements. Each should implement javax.xml.ws.handler.Handler or javax.xml.ws.handler.soap.SOAPHandler . These are more portable than CXF interceptors, but may cause the full message to be loaded in as a DOM (slower for large messages). |
jaxws:properties |
A properties map which should be supplied to the JAX-WS endpoint. See below. |
jaxws:dataBinding |
Which DataBinding to use in the endpoint. This can be supplied using the Spring <bean class="MyDataBinding"/> syntax. |
jaxws:binding |
You can specify the BindingFactory for this endpoint to use. This can be supplied using the Spring <bean class="MyBindingFactory"/> syntax. |
jaxws:conduitSelector |
|
Here is a more advanced example which shows how to provide interceptors, JAX-WS handlers, and properties:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Interceptors extend
e.g. org.apache.cxf.phase.AbstractPhaseInterceptor -->
<bean id="anotherInterceptor" class="..." />
<!-- Handlers implement
e.g. javax.xml.ws.handler.soap.SOAPHandler -->
<bean id="jaxwsHandler" class="..." />
<!-- The SOAP client bean -->
<jaxws:client id="helloClient"
serviceClass="demo.spring.HelloWorld"
address="http://localhost:9002/HelloWorld">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<ref bean="anotherInterceptor"/>
</jaxws:inInterceptor>
<jaxws:handlers>
<ref bean="jaxwsHandler" />
</jaxws:handlers>
<jaxws:properties>
<entry key="mtom-enabled" value="true"/>
</jaxws:properties>
</jaxws:client>
</beans>