码迷,mamicode.com
首页 > Web开发 > 详细

cxf开发基于web的webservice项目(转载)

时间:2014-12-30 18:32:26      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

其实开发服务端, 大体分为2种方式:
一: 采用jdk给我们提供的jas-ws中的服务类来发布服务
二: 采用第三方框架来开发webservice.
那么为什么我们要选择第三方框架来发布一个webservice服务呢?
首先, 我们开发的项目大部分都是javase项目, jdk不能用于javaee项目的开发. 
并且jdk目前仅仅支持soap1.1协议. 不支持soap1.2协议 而为了客户端调用时能使用1.1协议, 也能使用1.2协议.
通常我们发布的服务都是1.2协议的.
下面, 就说下cxf开发服务端, 以及开发客户端的最佳实践.
一: 利用cxf开发web服务端的最佳实践
在web.xml中配置cxfservlet共有3种方式:
1: 使用默认的配置文件
如果在配置cxfservlet的时候, 没有指定cxf配置文件的位置. cxf默认会到WEB-INF目录下去找一个叫cxf-servlet.xml的文件.
不过这种方式很少很少使用...(我至今没有发现有人采用这种方式的)
2: 配置cxfServlet的初始化参数指定配置文件(这种方式偶尔会用)
它存在2个问题: 一个是,servlet默认是在第一次访问的时候才会进行初始化, 这个时候才会去读取配置文件, 加载spring容器. 这就导致了第一次访问的时候速度太慢.
不过这个问题呢, 可以通过配置load-on-startup让cxfservlet在web容器启动的时候就去初始化, 从而避免了第一次访问速度太慢的问题.
第二个问题是, 我们开发常用ssh框架中, 已经在web.xml中配置了spring容器的监听器, 又在cxfservlet中配置, 会导致加载2次.
当然, 你也可以去掉spring的加载监听器.. 不过不建议使用.
3: 依赖spring的初始化监听器
这个方式是最常用的. 上代码

 1 <!-- 配置spring的文件位置 --> 
 2 <context-param> 
 3 <param-name>contextConfigLocation</param-name> 
 4 <param-value>classpath:applicationContext.xml</param-value> 
 5 </context-param> 
 6 
 7 <!-- 配置spring的容器监听器 --> 
 8 <listener> 
 9 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
10 </listener> 
11 
12 <!-- 配置cxf核心servlet --> 
13 <servlet> 
14 <servlet-name>CXFServlet</servlet-name> 
15 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 
16 <load-on-startup></load-on-startup> 
17 </servlet> 
18 
19 <servlet-mapping> 
20 <servlet-name>CXFServlet</servlet-name> 
21 <url-pattern>/cxf/*</url-pattern> 
22 </servlet-mapping> 

 


那么spring的配置文件到底该如何写呢?
在这之前, 先说下服务分类, 服务分为带接口的服务和不带接口的服务.
通常我们发布的服务都是带接口的服务, 如下:

 1 @WebService // 这个注解必须写, 否则服务中没有操作 
 2 @BindingType(SOAPBinding.SOAP12HTTP_BINDING)// 发布1.2soap协议 
 3 public interface IHiService { 
 4 
 5 String sayHi(String name); 
 6 }
 7 
 8 [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 9 public class HiServiceImpl implements IHiService { 
10 
11 public String sayHi(String name) { 
12 
13 System.out.println("HiServiceImpl.sayHi()"); 
14 return "hi " + name; 
15 } 
16 
17 } 

 

在spring配置文件中发服务,如下:

<!-- 导入cxf需要的xml文件 --> 
<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" /> 

<!-- 发布带接口的服务 --> 
<jaxws:server id="hi" serviceClass="com.cxf.service.IHiService" serviceName="hiWS" address="/hi"> 
<jaxws:serviceBean> 
<bean class="com.cxf.service.impl.HiServiceImpl"></bean> 
</jaxws:serviceBean> 
</jaxws:server>

 

 

至此服务端开发完毕.
二:利用cxf开发web客户端
注意这里是web客户端, 不是javase项目;
如果是javase项目,就 采用httpUrlConnection ,或者wsimport,或者手动构造soap请求协 等来进行开发.
如果是web项目, 最常用的发放有以下2种:
1: 利用spring的factory工厂来注入服务
2:利用cxf的标签来注入服务
不过这2种方法之前, 都需要使用wsimport, 或者wsdl2java命令, 获得web服务的接口.
获得接口如下:

 1 /** 
 2 * This class was generated by Apache CXF 2.4.0 
 3 * 2014-11-06T16:36:06.034+08:00 
 4 * Generated source version: 2.4.0 
 5 * 
 6 */ 
 7 
 8 @WebService(targetNamespace = "http://service.cxf.com/", name = "IHiService") 
 9 @XmlSeeAlso({}) 
10 public interface IHiService { 
11 
12 @WebResult(name = "return", targetNamespace = "") 
13 @RequestWrapper(localName = "sayHi", targetNamespace = "http://service.cxf.com/", className = "com.cxf.service.SayHi") 
14 @WebMethod 
15 @ResponseWrapper(localName = "sayHiResponse", targetNamespace = "http://service.cxf.com/", className = "com.cxf.service.SayHiResponse") 
16 public java.lang.String sayHi( 
17 @WebParam(name = "arg0", targetNamespace = "") 
18 java.lang.String arg0 
19 ); 
20 } 

 


然后, 再在spring的配置文件中配置:

 1 <!-- 方式一:使用jaxws:client来获得webservice服务 --> 
 2 <jaxws:client id="hiService" address="http://localhost:8080/cxf_javaee_0100_server/cxf/hi" serviceClass="com.cxf.service.IHiService"> 
 3 </jaxws:client> 
 4 
 5 
 6 <!-- 方式二: 使用spring的bean factory来获得webservice服务 --> 
 7 <bean id="hiService2" class="com.cxf.service.IHiService" factory-bean="webServiceFactory" factory-method="create"></bean> 
 8 
 9 <bean id="webServiceFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> 
10 <property name="address" value="http://localhost:8080/cxf_javaee_0100_server/cxf/hi"></property> 
11 <property name="serviceClass" value="com.cxf.service.IHiService"></property> 
12 </bean> 

 


方式一和方式二相比, 明显是方式一更加方便啊..
最后, 再启动spring容器来获得web服务, 然后调用服务了

1 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
2 IHiService hiService = (IHiService) context.getBean("hiService"); 
3 System.out.println(hiService.sayHi("aa")); 
4 
5 IHiService hiService2 = (IHiService) context.getBean("hiService2"); 
6 System.out.println(hiService2.sayHi("bb"));

 

控制台就会输出:
hi aa
hi bb

 

原文地址:http://blog.csdn.net/wangfeiing/article/details/40864301

cxf开发基于web的webservice项目(转载)

标签:

原文地址:http://www.cnblogs.com/maiyu-d/p/4194044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!