springmvc 集成apache cxf 开发webservice 示例

1.添加apache cxf 类库

[plain]  view plain  copy
  1. <span style="font-size:18px;"><!-- cxf webservice  -->  
  2.  <dependency>  
  3.       <groupId>org.apache.cxf</groupId>  
  4.       <artifactId>cxf-rt-frontend-jaxws</artifactId>  
  5.        <version>3.1.3</version>  
  6.  </dependency>  
  7.  <dependency>  
  8.        <groupId>org.apache.cxf</groupId>  
  9.        <artifactId>cxf-rt-transports-http</artifactId>  
  10.        <version>3.1.3</version>  
  11. </span><p><span style="font-size:18px;"></dependency></span></p>  

2.开发webservice接口

[java]  view plain  copy
  1. <span style="font-size:18px;">package com.sky.springmvc.webservice;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import javax.jws.WebParam;  
  7. import javax.jws.WebService;  
  8.   
  9. import com.sky.springmvc.vo.CustomVO;  
  10.   
  11. /** 
  12.  * 服务端接口 
  13.  * @author 孙效宁 
  14.  * 
  15.  */  
  16. @WebService  
  17. public interface HelloWorld {  
  18.     String sayHi(@WebParam(name="text") String text);  
  19.     CustomVO getCustomVO(@WebParam(name="id") String id);  
  20.     List<CustomVO> getCustomVOList();  
  21.     Map<String,CustomVO> getCutomMap();  
  22.     void save(@WebParam(name="customVO") CustomVO customVo);  
  23.     void saveCustomList(@WebParam(name="customVOList") List<CustomVO> customVOList);  
  24.     void saveCustomMap(@WebParam(name="customVOMap") Map<String,CustomVO> customVOMap);  
  25. </span><p><span style="font-size:18px;">}</span></p>  
3.开发webservic接口实现类

[java]  view plain  copy
  1. <span style="font-size:18px;">package com.sky.springmvc.webservice;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Date;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import javax.jws.WebService;  
  10.   
  11. import com.sky.springmvc.vo.CustomVO;  
  12.   
  13. /** 
  14.  * 服务端实现类 
  15.  * @author 孙效宁 
  16.  * 
  17.  */  
  18. @WebService(endpointInterface = "com.sky.springmvc.webservice.HelloWorld",serviceName="HelloWorld")  
  19. public class HelloWorldImpl implements HelloWorld {  
  20.   
  21.     public String sayHi(String text) {  
  22.         System.out.println("sayHi called");  
  23.         return "Hello " + text;  
  24.     }  
  25.   
  26.     @Override  
  27.     public CustomVO getCustomVO(String id) {  
  28.         System.err.println("id:"+id);  
  29.         CustomVO customVO = new CustomVO();  
  30.         customVO.setAge(22);  
  31.         customVO.setBirthDay(new Date());  
  32.         return customVO;  
  33.     }  
  34.   
  35.     @Override  
  36.     public List<CustomVO> getCustomVOList() {  
  37.         CustomVO customVO = new CustomVO();  
  38.         customVO.setAge(22);  
  39.         customVO.setBirthDay(new Date());  
  40.         List<CustomVO> customVOList = new ArrayList<CustomVO>();  
  41.         customVOList.add(customVO);  
  42.         return customVOList;  
  43.     }  
  44.   
  45.     @Override  
  46.     public Map<String, CustomVO> getCutomMap() {  
  47.         CustomVO customVO = new CustomVO();  
  48.         customVO.setAge(22);  
  49.         customVO.setBirthDay(new Date());  
  50.         Map<String,CustomVO> customMap = new HashMap<String,CustomVO>();  
  51.         customMap.put("1", customVO);  
  52.         return customMap;  
  53.     }  
  54.   
  55.     @Override  
  56.     public void save(CustomVO customVO) {  
  57.         System.err.println("age:"+customVO.getAge()+"birthday:"+customVO.getBirthDay());  
  58.           
  59.     }  
  60.   
  61.     @Override  
  62.     public void saveCustomList(List<CustomVO> customVOList) {  
  63.         System.err.println("size:"+customVOList.size());  
  64.           
  65.     }  
  66.   
  67.     @Override  
  68.     public void saveCustomMap(Map<String, CustomVO> customVOMap) {  
  69.         System.err.println("map size:"+customVOMap.size());  
  70.           
  71.     }  
  72. </span><p><span style="font-size:18px;">}</span></p>  
3.配置公开的服务

[html]  view plain  copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.  xmlns:jaxws="http://cxf.apache.org/jaxws"  
  4.  xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  5.  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  6.     <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  7.     <jaxws:endpoint id="helloWorld" implementor="com.sky.springmvc.webservice.HelloWorldImpl" address="/helloWorld"/>  
  8. </beans></span><span style="font-size:24px;">  
  9. </span>  

4.客户端调用webservice 服务示例

  方案一:

[java]  view plain  copy
  1. package com.sky.springmvc.webservice;  
  2.   
  3. import org.apache.cxf.interceptor.LoggingInInterceptor;  
  4. import org.apache.cxf.interceptor.LoggingOutInterceptor;  
  5. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  6. import org.junit.Test;  
  7. import org.springframework.context.ApplicationContext;  
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  9.   
  10. import com.sky.springmvc.vo.CustomVO;  
  11.   
  12. /** 
  13.  * webservice 调用示例 
  14.  * @author 孙效宁 
  15.  * 
  16.  */  
  17. public class Client {  
  18.        
  19.     /** 
  20.      * 使用配置文件形式,调用webserve 服务 
  21.      */  
  22.      @Test  
  23.      public void test1() {  
  24.         ApplicationContext app = new ClassPathXmlApplicationContext("config/spring/cxf-client.xml");  
  25.         HelloWorld hello = (HelloWorld) app.getBean("helloClient");  
  26.         hello.sayHi("hh");  
  27.      }  
  28.        
  29.      /** 
  30.       * 使用apache cxf api调用webservice 服务 
  31.       */  
  32.      @Test  
  33.      public void test2(){  
  34.          JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  35.          factory.getInInterceptors().add(new LoggingInInterceptor());  
  36.          factory.getOutInterceptors().add(new LoggingOutInterceptor());  
  37.          factory.setServiceClass(HelloWorld.class);  
  38.          factory.setAddress("http://localhost:8080/springmvc/webservices/helloWorld");  
  39.          HelloWorld client = (HelloWorld) factory.create();  
  40.             
  41.          String reply = client.sayHi("HI");  
  42.          System.out.println("Server said: " + reply);  
  43.          CustomVO customVO = client.getCustomVO("3");  
  44.          System.err.println("customVO:"+customVO);  
  45.      }  
  46. <p>}</p><p><pre name="code" class="html"><!-- 客户端源码配置 --><?xml version="1.0" encoding="UTF-8"?>  
  47. <beans xmlns="http://www.springframework.org/schema/beans"  
  48.        xmlns:jaxws="http://cxf.apache.org/jaxws"  
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  50.        xsi:schemaLocation="  
  51. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  52. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  53.    
  54. <jaxws:client id="helloClient"  
  55.                   serviceClass="com.sky.springmvc.webservice.HelloWorld"  
  56.                   address="http://localhost:8080/springmvc/webservices/helloWorld" />  
  57. </beans>  

 
方案1的问题:如何知道服务端的接口类型以及接口参数使用类型,接口返回参数类型? 


方案2::使用wsimport 工具生成客户端源码然后直接利用生成的客户端源码调用webservice服务,方案2不存在方案1存在的问题

1.生成客户端代码:

[java]  view plain  copy
  1. wsimport -s . -p com.sky.springmvc.wsutil.helloworld  http://localhost:8080/springmvc/webservices/helloWorld?wsdl  

2.利用客户端代码调用webservice 服务

[java]  view plain  copy
  1. package com.sky.springmvc;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Date;  
  5. import java.util.List;  
  6.   
  7. import org.junit.Test;  
  8.   
  9. import com.sky.springmvc.wsutil.helloWorld.CustomVO;  
  10. import com.sky.springmvc.wsutil.helloWorld.HelloWorld;  
  11. import com.sky.springmvc.wsutil.helloWorld.HelloWorld_Service;  
  12.   
  13. /** 
  14.  * 利用wsimport 工具生成的客户端调用webservice服务 
  15.  * @author 孙效宁 
  16.  * 
  17.  */  
  18. public class ClientDemo {  
  19.      @Test  
  20.      public void test1(){  
  21.           
  22.         HelloWorld helloWorld = new HelloWorld_Service().getHelloWorldImplPort();  
  23.         System.err.println("age:"+helloWorld.getCustomVO("3").getAge());  
  24.         System.err.println("list:"+helloWorld.getCustomVOList());  
  25.         System.err.println("map:"+helloWorld.getCutomMap());  
  26.         CustomVO customVO = new CustomVO();  
  27.         customVO.setAge(22);  
  28.         customVO.setBirthDay(new DateTest().convertToXMLGregorianCalendar(new Date()));  
  29.         helloWorld.save(customVO);  
  30.         List<CustomVO> customVOList = new ArrayList<CustomVO>();  
  31.         customVOList.add(customVO);  
  32.         helloWorld.saveCustomList(customVOList);  
  33.      }  
  34. }  

最重要的配置文件web.xml文件差点儿给忘了

[html]  view plain  copy
  1. <!-- cxf webservice -->  
  2.     <servlet>  
  3.         <description>Apache CXF Endpoint</description>  
  4.         <display-name>cxf</display-name>  
  5.         <servlet-name>cxf</servlet-name>  
  6.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  7.         <load-on-startup>2</load-on-startup>  
  8.     </servlet>  
  9.     <servlet-mapping>  
  10.         <servlet-name>cxf</servlet-name>  
  11.         <url-pattern>/webservices/*</url-pattern>  
  12.     </servlet-mapping>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值