`

CXF service的几种使用方法

阅读更多
以下以服务端和客户端列举几种CXF的常用方法:
服务器端:

 

方法一:使用Endpoint.publish进行发布,这种方式不需要其他的额外的包,也不需要进行配置文件的书写,可以用于底层属jdbc型的应用程序,其中第一个参数为访问时需要调用的功用地址,第二个参数为要发布的接口的实现类(此种方法非Cxf方式)
Endpoint endpoint =
 Endpoint.publish("http://localhost:8080/helloService",new HelloServiceImpl());
System.out.println("WS发布成功!");
  

 

方法二:用CXF的JaxWsServerFactoryBean类进行发布,使用这种方式发布,需要导入CXF相关的jar包,但是不需要进行相关配置文件的书写,其中address为发布的地址,serviceClass为接口类

 

 

HelloServiceImpl impl = new HelloServiceImpl();
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setAddress("http://localhost:8080/WSCXF/helloService");//发布地址
factoryBean.setServiceClass(IHelloService.class);//接口类
factoryBean.setServiceBean(impl);//发布接口的实现类的实例
factoryBean.create();
System.out.println("WS发布成功!");
 

 

 

方法三:使用配置式的,该方法既需要导入CXF的相关jar包,也需要进行配置文件的书写,这种方法适用于与spring整合的应用中,因为可以将bean托管给spring,也就可以随心所欲的使用spring中配置的bean了,以下是application-server.xml文件的配置信息

 

 

<?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-2.5.xsd
	http://cxf.apache.org/jaxws
	http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <!--cxf配置信息-->
    <bean id="helloServiceBean" class="org.cxf.service.HelloServiceImpl"/>
    <jaxws:endpoint id="helloService" implementor="#helloServiceBean" address="/hello">
    </jaxws:endpoint>

</beans>

 

 

 

客户端:

 

方法一:使用CXF中 JaxWsProxyFactoryBean客户端代理工厂调用web服务,这种方法需要导入cxf相关的jar包,由于需要显式调用暴露方法的接口来生成一个实例,因此该方法适用于服务器端与客户端处于同一个应用的相同服务下的应用程序

 

JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
soapFactoryBean.setAddress("http:// localhost:8080/helloService");
soapFactoryBean.setServiceClass(IHelloService.class);
Object o = soapFactoryBean.create();
IHelloService helloService = (IHelloService)o;
helloService.sayHello();//sayHello()为暴露的接口中的一个方法;

 

 

方法二:这种方法需要导入相关CXF的jar包,该方法的好处是只需要知道相关的暴露接口中的方法名,既可以隐式调用,适合于发布的服务器和调用的客户端不处于同一应用,同一服务下的应用程序,注意
http://localhost:8080/WSCXF/helloService   //为暴露的接口的名称
?wsdl 必须要添加,这个是接口解析成的xml文件

 

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = 
   dcf.createClient("http://localhost:8080/WSCXF/helloService?wsdl");

//sayHello为接口中定义的方法名称张三为传递的参数返回一个Object数组
Object[] objects=client.invoke("sayHello", "张三");

//输出调用结果
System.out.println(objects[0].toString());

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics