基于Spring和CXF的webservice开发环境搭建

使用CXF发布webservice服务时,规范的做法是先书写一个接口,用以声明服务类型。

基于spring和CXF开发web service的框架搭建

一、创建web项目

Eclipse中新建一个dynamic webproject,命名为:CXFTest

二、导入需要的jar包

把下载的CXF项目的解压缩文件中lib文件夹下的所有jar包拷贝到WebContent->WEB-INF->lib文件夹下

三、创建服务接口

Java resource->src目录下新建package包:com.cxfspring.service,并在该包中新建服务接口Ipeople,Ipeople接口代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.cxfspring.service;  
  2.    
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebService;  
  5.    
  6. @WebService  
  7. public interface IPeople  
  8. {  
  9.     @WebMethod  
  10.     public String sayHello(String name);  
  11. }  


四、创建服务实现类

在Java resource->src目录下新建package包:com.cxfspring.serviceImpl,并在该包中新建服务实现类Student,Student实现Ipeople接口,Student类代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.cxfspring.serviceImpl;  
  2.    
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebService;  
  5.    
  6. import com.cxfspring.service.IPeople;  
  7.    
  8.    
  9. @WebService  
  10. public class Student implements IPeople  
  11. {  
  12.    
  13.          @WebMethod  
  14.          @Override  
  15.          publicString sayHello(String name)  
  16.          {  
  17.                    //TODO Auto-generated method stub  
  18.                    System.out.println("Hello:"+name);  
  19.                   return"Hello:"+name+"nice to meet you!";  
  20.          }  
  21.    
  22. }  


五、配置web.xml文件

在WebContent->WEB-INF目录下新建web.xml,配置代码如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0"encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3. xmlns="http://java.sun.com/xml/ns/javaee"  
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  6. version="3.0">  
  7.   <display-name>ServiceManagement</display-name>  
  8.    
  9.   <!-- 加载Spring容器 -->  
  10.    
  11.   <listener>  
  12.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  13.   </listener>  
  14.    
  15.   <!-- 配置CXF的核心servlet -->  
  16.   <context-param>  
  17. <param-name>contextConfigLocation</param-name>  
  18. <param-value>/WEB-INF/applicationContext.xml</param-value>  
  19. </context-param>  
  20.   <servlet>  
  21.     <servlet-name>cxf</servlet-name>  
  22.     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  23.     <!-- <init-param>  
  24.         配置Spring的配置文件  
  25.         <param-name>config-location</param-name>  
  26.         <param-value>/WEB-INF/applicationContext.xml</param-value>  
  27.     </init-param>-->  
  28.     <load-on-startup>1</load-on-startup>  
  29.   </servlet>  
  30.   <servlet-mapping>  
  31.     <servlet-name>cxf</servlet-name>  
  32.     <url-pattern>/*</url-pattern>  
  33.   </servlet-mapping>  
  34.    
  35.   <welcome-file-list>  
  36.    
  37.     <welcome-file>index.jsp</welcome-file>  
  38.   </welcome-file-list>  
  39. </web-app>  


六、配置Spring配置文件

在WebContent->WEB-INF目录下新建applicationContext.xml,配置代码如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0"encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4. xmlns:tx="http://www.springframework.org/schema/tx"  
  5. xmlns:jaxws="http://cxf.apache.org/jaxws"  
  6. xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
  7. xmlns:jms="http://cxf.apache.org/transport/jms"  
  8. xmlns:context="http://www.springframework.org/schema/context"  
  9. xmlns:aop="http://www.springframework.org/schema/aop"  
  10. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  12. http://www.springframework.org/schema/tx  
  13. http://www.springframework.org/schema/tx/spring-tx.xsd  
  14. http://www.springframework.org/schema/context  
  15. http://www.springframework.org/schema/tx/spring-aop.xsd  
  16. http://www.springframework.org/schema/beans  
  17. http://www.springframework.org/schema/context/spring-context.xsd  
  18. http://cxf.apache.org/jaxws  
  19. http://cxf.apache.org/schemas/jaxws.xsd  
  20. http://cxf.apache.org/jaxrs  
  21. http://cxf.apache.org/schemas/jaxrs.xsd  
  22. http://cxf.apache.org/transport/jms  
  23. http://cxf.apache.org/schemas/configuration/jms">  
  24.    
  25. <!-- 导入CXF为扩展Spring提供的几个XML配置文件 -->  
  26. <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  27. <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
  28. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
  29. <import resource="classpath:META-INF/cxf/cxf-extension-object-binding.xml"/>  
  30.    
  31. <!-- 利用endpoint发布服务 -->  
  32. <jaxws:endpoint id="HelloService"implementor="com.cxfspring.serviceImpl.Student" address="/HelloService"/>  
  33.    
  34. </beans>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值