`

Webservice实战

 
阅读更多

参考链接:http://cxf.apache.org/docs/writing-a-service-with-spring.html

先说下cxf是什么东西吧:

Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

 

一、SEI的定义

假设有以下SEI定义:

Java代码  收藏代码
  1. @WebService  
  2. public interface OrderProcess {  
  3.     public String processOrder(Order order);  
  4. }  

 (实现端省略)

 

二、Server端发布

则最简单的发布Server的方式可以如下:

 

Java代码  收藏代码
  1. Endpoint.publish("http://localhost:8181/orderProcess"new OrderProcessImpl());  

 

或者是spring的方式:

Xml代码  收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="  
  5.        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd  
  6.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  8.   
  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.   
  13.     <jaxws:endpoint id="orderProcess"  
  14.         implementor="com.liulutu.liugang.cxf.jaxws.OrderProcessImpl" address="http://localhost:8090/orderProcess" />  
  15. </beans>  

 三、Client端调用

Java代码  收藏代码
  1. Service service = Service.create(new URL("<wsdlfilepath>"),  
  2.         new QName("namespace""servicename"));  
  3. OrderProcess port = orderProcessService.getPort(OrderProcess.class);  
  4. String s = port.processOrder(<arg>);  
  5. System.out.println(s);  

 或者Spring的方式:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:jaxws="http://cxf.apache.org/jaxws"  
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.        xsi:schemaLocation="  
  6.           http://www.springframework.org/schema/beans   
  7.           http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.           http://cxf.apache.org/jaxws   
  9.           http://cxf.apache.org/schemas/jaxws.xsd">  
  10.    
  11.     <jaxws:client id="orderClient"  
  12.                   serviceClass="com.liulutu.liugang.cxf.codefirst.OrderProcess"  
  13.                   address="http://localhost:8181/orderProcess" />  
  14. </beans>  

 然后在java代码里:

Java代码  收藏代码
  1. ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext(  
  2.       "/META-INF/spring/jaxwsspringclient.xml");  
  3. OrderProcess bean = xmlApplicationContext.getBean(OrderProcess.class);  
  4. System.out.println(bean.processOrder(<order>)); 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics