转自:https://www.aliyun.com/zixun/wenji/1263190.html

CXF服务端:

  1. package com.sean.server;
  2. import javax.jws.WebParam;
  3. import javax.jws.WebService;
  4. @WebService
  5. public interface Plus {
  6. public int add(@WebParam(name="x") int x, @WebParam(name="y") int y);
  7. }
  1. package com.sean.server;
  2. public class PlusImpl implements Plus {
  3. public int add(int x, int y){
  4. return x + y;
  5. }
  6. }
  1. package com.sean.server;
  2. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
  3. import org.apache.cxf.frontend.ServerFactoryBean;
  4. public class Server {
  5. public static void main(String args[]) throws Exception {
  6. PlusImpl plusImpl = new PlusImpl();
  7. JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
  8. //ServerFactoryBean factory = new ServerFactoryBean();
  9. factory.setServiceClass(Plus.class);
  10. factory.setAddress("http://127.0.0.1:8888/Plus");
  11. factory.setServiceBean(plusImpl);
  12. factory.create();
  13. }
  14. }

程序启动后,访问http://127.0.0.1:8888/Plus?wsdl即可查看自动生成的WSDL文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <wsdl:definitions targetNamespace="http://server.sean.com/"
  3. name="PlusService" xmlns:ns1="http://schemas.xmlsoap.org/soap/http"
  4. xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  5. xmlns:tns="http://server.sean.com/"
  6. xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  7. xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  8. <wsdl:types>
  9. <xs:schema targetNamespace="http://server.sean.com/"
  10. xmlns:tns="http://server.sean.com/" version="1.0"
  11. elementFormDefault="unqualified"
  12. xmlns:xs="http://www.w3.org/2001/XMLSchema">
  13. <xs:element name="add" type="tns:add"/>
  14. <xs:element name="addResponse" type="tns:addResponse"/>
  15. <xs:complexType name="add">
  16. <xs:sequence>
  17. <xs:element name="x" type="xs:int"/>
  18. <xs:element name="y" type="xs:int"/>
  19. </xs:sequence>
  20. </xs:complexType>
  21. <xs:complexType name="addResponse">
  22. <xs:sequence>
  23. <xs:element name="return" type="xs:int"/>
  24. </xs:sequence>
  25. </xs:complexType>
  26. </xs:schema>
  27. </wsdl:types>
  28. <wsdl:message name="addResponse">
  29. <wsdl:part name="parameters" element="tns:addResponse"> </wsdl:part>
  30. </wsdl:message>
  31. <wsdl:message name="add">
  32. <wsdl:part name="parameters" element="tns:add"> </wsdl:part>
  33. </wsdl:message>
  34. <wsdl:portType name="Plus">
  35. <wsdl:operation name="add">
  36. <wsdl:input name="add" message="tns:add"> </wsdl:input>
  37. <wsdl:output name="addResponse" message="tns:addResponse"> </wsdl:output>
  38. </wsdl:operation>
  39. </wsdl:portType>
  40. <wsdl:binding name="PlusServiceSoapBinding" type="tns:Plus">
  41. <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
  42. <wsdl:operation name="add">
  43. <soap:operation style="document" soapAction=""/>
  44. <wsdl:input name="add">
  45. <soap:body use="literal"/>
  46. </wsdl:input>
  47. <wsdl:output name="addResponse">
  48. <soap:body use="literal"/>
  49. </wsdl:output>
  50. </wsdl:operation>
  51. </wsdl:binding>
  52. <wsdl:service name="PlusService">
  53. <wsdl:port name="PlusPort" binding="tns:PlusServiceSoapBinding">
  54. <soap:address location="http://127.0.0.1:8888/Plus"/>
  55. </wsdl:port>
  56. </wsdl:service>
  57. </wsdl:definitions>

如果服务端使用ServerFactoryBean类,则最终生成的WSDL文件略有不同

CXF客户端:

如果服务端使用ServerFactoryBean类,则客户端需要使用JaxWsServerFactoryBean类

如果服务端使用JaxWsServerFactoryBean类,则客户端需要使用JaxWsProxyFactoryBean类

  1. package com.sean.client;
  2. import org.apache.cxf.frontend.ClientProxyFactoryBean;
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  4. import com.sean.server.Plus;
  5. public class Client {
  6. public static void main(String[] args) {
  7. //ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
  8. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  9. factory.setAddress("http://127.0.0.1:8888/Plus");
  10. Plus client = factory.create(Plus.class);
  11. System.out.println(client.add(2, 2));
  12. System.exit(0);
  13. }
  14. }

无论服务端使用ServerFactoryBean类还是JaxWsServerFactoryBean类,都可在客户端使用JaxWsDynamicClientFactory类,并通过反射的方式调用WebService服务端

  1. package com.sean.client;
  2. import org.apache.cxf.endpoint.Client;
  3. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
  4. public class Client2 {
  5. public static void main(String[] args) throws Exception {
  6. JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
  7. Client client = factory.createClient("http://127.0.0.1:8888/Plus?wsdl");
  8. Object[] inputs = {1, 2};
  9. Object[] result = client.invoke("add", inputs);
  10. System.out.println(result[0]);
  11. }
  12. }

基于Apache CXF的Web Service服务端/客户端的更多相关文章

  1. 基于JAX-WS的Web Service服务端/客户端 ;JAX-WS + Spring 开发webservice

    一.基于JAX-WS的Web Service服务端/客户端 下面描述的是在main函数中使用JAX-WS的Web Service的方法,不是在web工程里访问,在web工程里访问,参加第二节. JAX ...

  2. 使用Eclipse自带Web Service插件(Axis1.4)生成Web Service服务端/客户端

    创建一个名字为math的Java web工程,并将WSDL文件拷入该工程中 将Axis所需的jar包拷贝至WebRoot\WEB-INF\lib目录下,这些jar包会自动导入math工程中 一,生成W ...

  3. Eclipse+Axis使用WSDL文件生成Web Service服务端/客户端

    JDK版本:1.5.0_22 Eclipse版本:Helios Service Release 2(3.6.2) WSDL文件的创建过程见http://blog.csdn.net/a19881029/ ...

  4. 使用Eclipse自带的Axis1插件生成Web Service服务端客户端

    JDK版本:1.5.0_22 Eclipse版本:Helios Service Release 2(3.6.2) WSDL文件的创建过程见http://blog.csdn.net/a19881029/ ...

  5. 使用CXF开发Web Service服务

    1.使用CXF开发Web Service服务端 1.1 开发一个Web Service业务接口,该接口要用@WebService修饰 (1)创建一个Java项目MyServer (2)在MyServe ...

  6. Apache CXF实现Web Service(5)—— GZIP使用

    Apache CXF实现Web Service(5)-- GZIP使用 参考来源: CXF WebService整合Spring Apache CXF实现Web Service(1)--不借助重量级W ...

  7. 使用axis开发web service服务端

    一.axis环境搭建 1.安装环境 JDK.Tomcat或Resin.eclipse等. 2.到 http://www.apache.org/dyn/closer.cgi/ws/axis/1_4下载A ...

  8. Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service

    起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...

  9. Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service

    实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http: ...

随机推荐

  1. Memcached 与 Redis 的关键性能指标比较

    性能对比: Redis 只使用单核,而 Memcached 可以使用多核,所以平均每一个核上 Redis在存储小数据时比 Memcached 性 能更高. 而在 100k 以上的数据中,Memcach ...

  2. vue-cli简介(中文翻译)

    vue-cli是一个简单的vuejs脚手架命令行工具. 安装 准备:Node.js(>=4.x,推荐6.x版本),npm版本3以上和Git. $npm install -g vue-cli 使用 ...

  3. 利用JavaScript的%做隔行换色

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  4. Nginx下修改php.ini后重新加载配置文件命令

    修改php.ini后 如,我的 php.ini 文件是放在 /etc/php.ini php 所在目录是 /www/Linux/php-5.2.17 修改 php.ini 后要用 php-fpm 来进 ...

  5. 了解权限控制框架shiro 之实际应用.

    Apache Shiro 1.权限控制分为 a.粗粒度 URL 级别权限控制     b.细粒度方法级别权限控制 2.使用shiro进行权限控制主要有四种主要方式 : a. 在程序中 通过 Subje ...

  6. jQuery删除元素

    remove() - 删除被选元素(及其子元素) empty() - 从被选元素中删除子元素 $("#div1").remove();删除被选元素及其子元素. $("#d ...

  7. MyBatis 基础入门

    MyBatis 是一个半自动化的持久层的框架,能让开发者专注SQL本身 JDBC 连接数据库的硬编码问题,通过config,mapper配置文件解决 Mybatis开发需要关注的文件 l POJO类( ...

  8. js中时间戳与日期时间之间的相互转换

    1.时间戳转换为标准日期时间格式: function timeFormat(dateStr) { var date = new Date(dateStr); Y = date.getFullYear( ...

  9. centos7的编译安装php5.3 (针对老系统必须安装php5.3才能运行)

    大家都知道,centos6以上yum都自带5.4以上的php版本,可是一些老系统必须安装 php-5.3该怎么办呢.下面我来教大家一步步编译安装.看看我踩过的坑. 第一步: 网上下载php5.3的源码 ...

  10. struct timeval和gettimeofday

    struct timeval和gettimeofday() struct timeval结构体在time.h中的定义为: struct timeval { time_t tv_sec; /* Seco ...