CXF提供了很多client端的调用方法。官方提供的文档可以让你快速了解这些选择,帮助你判断使用哪种。
参考地址:http://cxf.apache.org/docs/index.html
本文将介绍两种方式:
1、Client Proxy
使用JaxWsProxyFactoryBean类简化Proxy。
2、Dynamic Client
不需要SEI接口类,JaxWsDynamicClientFactory.newInstance获得JaxWsDynamicClientFactory实例,
通过dcf.createClient获得Client客户端引用。
参考地址:http://cxf.apache.org/docs/index.html
本文将介绍两种方式:
首先本项目使用mavent管理,通过cxf-codegen-plugin生成客户端代码
<properties>
<cxf.version>3.0.3</cxf.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/services</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>http://localhost:8889/ms?wsdl</wsdl>
<extraargs>
<extraarg>-impl</extraarg>
<extraarg>-verbose</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
1、Client Proxy
使用JaxWsProxyFactoryBean类简化Proxy。
public static void main(String[] args) {
//使用CXF的方式访问
JaxWsProxyFactoryBean fac = new JaxWsProxyFactoryBean();
fac.setServiceClass(IPersonService.class);
fac.setAddress("http://localhost:8889/ms");
IPersonService ms = (IPersonService) fac.create();
Person person = ms.getPersonInfos();
System.out.println("name:" + person.getName() + ",sex:" + person.getSex() + ",email:" + person.getEmail());
}
2、Dynamic Client
不需要SEI接口类,JaxWsDynamicClientFactory.newInstance获得JaxWsDynamicClientFactory实例,
通过dcf.createClient获得Client客户端引用。
@Test
public void test002() throws Exception{
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8889/ms?wsdl");
Object[] objects = client.invoke("getPersonInfos");
Person person = (Person) objects[0];
System.out.println("name:" + person.getName() + ",sex:" + person.getSex() + ",email:" + person.getEmail());
}