In the previous posts we saw how to run a CXF application and test the web services using a JAX-WS client. While our previous client was built using classes from the JAX-WS API, there are other ways to execute the code too.
Consider the below client:
From the source code:
This configuration can be further simplified:
Consider the below client:
public static void main(String[] args) throws MalformedURLException { final String endpointAddress = "http://localhost:8080/WithSpring/services/echo"; JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(IEcho.class); //the service SEI factory.setAddress(endpointAddress); IEcho client = (IEcho) factory.create(); String reply = client.echoHi("Dude"); System.out.println("Server said: " + reply); }The below shorter code will produce the same response. It uses a org.apache.cxf.jaxws.JaxWsProxyFactoryBean to create the client stub. This is a CXF class.
From the source code:
Factory for creating JAX-WS proxies, This class provides access to the internal properties used to set-up proxies. Using it provides more control than the standard JAX-WS APIs.If we are using Spring on the client, then the JaxWsProxyFactoryBean can be configured as a bean. In fact the stub can also be set up as a bean:
public static void main(String[] args) throws MalformedURLException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "client-beans.xml"); IEcho client = (IEcho) context.getBean("client"); System.out.println(client.echoHi("Joe")); }The details are all in the spring configuration file:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 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/schema/jaxws.xsd"> <bean id="client" class="com.code.first.ws.server.IEcho" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.code.first.ws.server.IEcho" /> <property name="address" value="http://localhost:8080/WithSpring/services/echo" /> </bean> </beans>We configured the JaxWsProxyFactoryBean and then created a client bean specifying the factory instance method to be used to instantiate it.
This configuration can be further simplified:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 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="client" serviceClass="com.code.first.ws.server.IEcho" address="http://localhost:8080/WithSpring/services/echo" /> </beans>In this case we used the client element from the http://cxf.apache.org/jaxws namespace. The element is a shortcut technique for the earlier style. It also creates a bean which we used in our main method.
No comments:
Post a Comment