Proxying JAX-WS Services in Spring
by, 11-28-2011 at 12:25 AM (6032 Views)
I have been exploring JAX-WS services in Spring. In this tip, I will look into proxying JAX-WS services on the client side. Consuming web services with Spring involves client-side proxies in a way similar to how Spring based clients consume other remoting technologies.
Using JaxWsPortProxyFactoryBean is used to wire the Car web service in Spring similar to any other Spring bean. The JaxWsPortProxyFactoryBean is a factory bean that produces a proxy that knows how to talk to a SOAP web service. It will create a proxy implementing the serviceís interface. Therefore with the JaxWsPortProxyFactoryBean you can wire and use a remote web service as if it were any local POJO. If we are configuring the Car web service with the JaxWsPortProxyFactoryBean, it would look like this:
There are several properties of JaxWsPortProxyFactoryBean must be set in order to work. The wsdlDocumentUrl property identifies the location of the remote web serviceís definition file; JaxWsPortProxyFactoryBean uses the WSDL available at that URL to construct a proxy to the service; And the proxy thatís produced by JaxWsPortProxyFactoryBean will implement the CarService interface, as specified by the serviceInterface property. Finallythe remaining three properties can usually be determined by looking at the serviceís WSDL. If the car service looked like this:Java Code:<bean id="carService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean" p:wsdlDocumentUrl="http://localhost:8080/services/CarService?wsdl" p:serviceName="carrService" p:portName="carrServiceHttpPort" p:serviceInterface="com.acme.springexample.service.CarService" p:namespaceUri="http://springexample.com"/>
Itís possible for multiple services and/or ports to be defined in the serviceís WSDL. The JaxWsPortProxyFactoryBean requires that the port be specified and service names in the portName and serviceName properties. If you check the <wsdl:port> and <wsdl:service> to determine how to set these properties. The last piece is the namespaceUri property specifies the namespace of the service which will help JaxWsPortProxyFactoryBean locate the service definition in the WSDL. In most cases it is the targetNamespace attribute of the <wsdl:definitions> element.Java Code:<wsdl:definitions targetNamespace="http://springexample.com"> ... <wsdl:service name="carService"> <wsdl:port name="carServiceHttpPort" binding="tns:spitterServiceHttpBinding"> ... </wsdl:port> </wsdl:service> </wsdl:definitions>