0.本文是基于上一篇文章CXF之Simple服务器搭建,所以跳过pom.xml和web.xml的配置部分
1.在客户端编写一个和服务端的接口一样的接口类
package com.tangjun.ws.cxf.service.interfaces;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHi();
}
当然,如果不跨平台的话。其实也可以直接把服务端的类直接拿过来。
2.客户端调用服务
客户端调用服务有很多种方式,但总的看来有两大思路:
其一,通过JaxWsProxyFactoryBean来获取服务接口类,这种方法还分通过spring配置来获取和通过java代码来获取
其二,通过jaxws:client标签来自动生成
下面主要说三种方式:
a.spring配置jaxws:client
<?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="helloWorldClient" serviceClass="com.tangjun.ws.cxf.service.interfaces.HelloWorld"
address="http://localhost:9090/exmaple_cxf_server/services/helloWorld" />
</beans>
<jaxws:client>标签会自动生成一个bean,调用的时候直接根据id来获取bean就可以了
package com.tangjun.ws.cxf.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tangjun.ws.cxf.service.interfaces.HelloWorld;
public class JaxWsClientTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext(new String[] {"spring-cxf-client.xml"});
HelloWorld service = (HelloWorld) context.getBean("helloWorldClient");
System.out.println(service.sayHi());
}
}
b.spring配置serviceProxy
<?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="helloWorldClient" serviceClass="com.tangjun.ws.cxf.service.interfaces.HelloWorld"
address="http://localhost:9090/exmaple_cxf_server/services/helloWorld" />
<bean id="serviceProxy" class="com.tangjun.ws.cxf.service.interfaces.HelloWorld" factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.tangjun.ws.cxf.service.interfaces.HelloWorld"/>
<property name="address" value="http://localhost:9090/exmaple_cxf_server/services/helloWorld"/>
</bean>
</beans>
调用的时候根据ID来获取bean就可以了
package com.tangjun.ws.cxf.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tangjun.ws.cxf.service.interfaces.HelloWorld;
public class SpringBeanTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext(new String[] {"spring-cxf-client.xml"});
HelloWorld service = (HelloWorld) context.getBean("serviceProxy");
System.out.println(service.sayHi());
}
}
c.java代码获取serviceProxy
package com.tangjun.ws.cxf.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.tangjun.ws.cxf.service.interfaces.HelloWorld;
public class JaxWsProxyFactoryTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean fac = new JaxWsProxyFactoryBean();
fac.setServiceClass(HelloWorld.class);
fac.setAddress("http://localhost:9090/exmaple_cxf_server/services/helloWorld");
HelloWorld service = (HelloWorld) fac.create();
System.out.println(service.sayHi());
}
}
3.测试结果
>helloworld, Wed Apr 22 16:20:54 CST 2015