【转】使用Apache CXF开发WebServices服务端

原地址:http://cnjava.blog.51cto.com/1208887/335630

 

在前一篇的博客中,我使用Xfire1.x来开发了WebServies的服务端。

但是如果你访问Apache的官网,可以看到xfire已经被合并了。

最新的框架叫做CXF。 
Apache CXF = Celtix + XFire。 
CXF 继承了 Celtix 和 XFire 两大开源项目的精华, 
提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。

总之,就是cxf就是好啦。那么接下来我们使用cxf来开发一个webServices的服务端。体验CXF的好处。

环境:MyEclipse6.01+jdk5

1)首先,建一个web工程.

2)写好一个接口和服务类

//服务接口IHelloService.java

 

 1 package com.pengzj.service;
 2 
 3 import java.util.List;
 4 
 5 import javax.jws.WebMethod; 
 6 import javax.jws.WebService;
 7 
 8 /** 
 9 * 
10 * @author atixujie#gz-accp 
11 * 
12 */ 
13 @WebService 
14 public interface IHelloService {
15 
16     @WebMethod 
17     public String sayHi(String uname); 
18 }

 

 

 

//webServices的实现类IHelloServiceImpl .java

 

 1 import com.pengzj.service.IHelloService;
 2 
 3 @WebService 
 4 public class IHelloServiceImpl implements IHelloService{
 5 
 6     @Override 
 7     @WebMethod 
 8     public String sayHi(String uname) { 
 9         return "Hello "+uname; 
10     }
11 
12 }

 

 

 

大家可以注意到,这里用到了webServices的注解。@WebService和@WebMethod.

3)导入CXF的jar包。

到apache的网站上下载CXF的包。

最新的版本是2.2.9 。当然如果你下载这个包就会比较麻烦。因为它需要最新的JDk(1.6.01都不行。要什么1.6.u11)的支持。

所以建议你还是下载2.0.4地址如下:

http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip

解压之后将lib文件夹下的jar包都复制到项目中。

4)配置CXF。

然后将web.xml的配置文件改成如下:

 

 1 <display-name>cxf</display-name> 
 2     <description>cxf</description> 
 3     <servlet> 
 4         <description>Apache CXF Endpoint</description> 
 5         <servlet-name>cxf</servlet-name> 
 6         <servlet-class> 
 7             org.apache.cxf.transport.servlet.CXFServlet 
 8         </servlet-class> 
 9         <load-on-startup>1</load-on-startup> 
10     </servlet>
11 
12     <servlet-mapping> 
13         <servlet-name>cxf</servlet-name> 
14         <url-pattern>/services/*</url-pattern> 
15     </servlet-mapping> 
16     <session-config> 
17         <session-timeout>60</session-timeout> 
18     </session-config>
19 
20     <listener> 
21         <listener-class> 
22             org.springframework.web.context.ContextLoaderListener 
23         </listener-class> 
24     </listener>

 

 

 

 

5)配置Spring的配置文件。

因为CXF集成了Spring。所以以上的配置要默认到WEB-INF/下找spring的配置文件applicationContext.xml。

所以我们要在WEB-INF下建立一个Spring的配置文件applicationContext.xml如下:

 

 1 <?xml version="1.0" encoding="UTF-8"?> 
 2 <beans 
 3     xmlns="http://www.springframework.org/schema/beans" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xmlns:jaxws="http://cxf.apache.org/jaxws" 
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
 7 http://cxf.apache.org/jaxws 
 8 http://cxf.apache.org/schemas/jaxws.xsd"> 
 9      <import resource="classpath:META-INF/cxf/cxf.xml"/> 
10     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> 
11     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> 
12     <!-- 配置好webservices的类名和服务名 --> 
13     <jaxws:endpoint id="hellows" 
14         implementor="com.pengzj.service.impl.IHelloServiceImpl" address="/Hellows" />
15 
16 </beans>

 

 

 

 

准备工作结束了。

部署,运行。在地址上输入:http://localhost:8080/cxfws_0619/services/

就应该可以看到一个超链接,点击可以看到如下的wsdl-xml文件

1 <?xml version="1.0" encoding="utf-8" ?>
2 
3 + <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://service.pengzj.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.pengzj.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IHelloServiceImplService" targetNamespace="http://impl.service.pengzj.com/">

 

ok.使用CXF开发服务端大功告成。

总结步骤:

1)建立web工程。导入cxf的jar包。

2)配置web.xml文件

3)配置spring的配置文件。同时配置好服务类的bean.

4)部署运行。

在下一篇中,我们将介绍利用wsdl2java工具生成代码,完成客户端的调用。

转载于:https://www.cnblogs.com/zhengyutao/archive/2012/12/12/2814334.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值