基于Apache CXF的Web Service服务端/客户端
转自:https://www.aliyun.com/zixun/wenji/1263190.html
CXF服务端:
- package com.sean.server;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface Plus {
- public int add(@WebParam(name="x") int x, @WebParam(name="y") int y);
- }
- package com.sean.server;
- public class PlusImpl implements Plus {
- public int add(int x, int y){
- return x + y;
- }
- }
- package com.sean.server;
- import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
- import org.apache.cxf.frontend.ServerFactoryBean;
- public class Server {
- public static void main(String args[]) throws Exception {
- PlusImpl plusImpl = new PlusImpl();
- JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
- //ServerFactoryBean factory = new ServerFactoryBean();
- factory.setServiceClass(Plus.class);
- factory.setAddress("http://127.0.0.1:8888/Plus");
- factory.setServiceBean(plusImpl);
- factory.create();
- }
- }
程序启动后,访问http://127.0.0.1:8888/Plus?wsdl即可查看自动生成的WSDL文件
- <?xml version="1.0" encoding="UTF-8"?>
- <wsdl:definitions targetNamespace="http://server.sean.com/"
- name="PlusService" xmlns:ns1="http://schemas.xmlsoap.org/soap/http"
- xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
- xmlns:tns="http://server.sean.com/"
- xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xs:schema targetNamespace="http://server.sean.com/"
- xmlns:tns="http://server.sean.com/" version="1.0"
- elementFormDefault="unqualified"
- xmlns:xs="http://www.w3.org/2001/XMLSchema">
- <xs:element name="add" type="tns:add"/>
- <xs:element name="addResponse" type="tns:addResponse"/>
- <xs:complexType name="add">
- <xs:sequence>
- <xs:element name="x" type="xs:int"/>
- <xs:element name="y" type="xs:int"/>
- </xs:sequence>
- </xs:complexType>
- <xs:complexType name="addResponse">
- <xs:sequence>
- <xs:element name="return" type="xs:int"/>
- </xs:sequence>
- </xs:complexType>
- </xs:schema>
- </wsdl:types>
- <wsdl:message name="addResponse">
- <wsdl:part name="parameters" element="tns:addResponse"> </wsdl:part>
- </wsdl:message>
- <wsdl:message name="add">
- <wsdl:part name="parameters" element="tns:add"> </wsdl:part>
- </wsdl:message>
- <wsdl:portType name="Plus">
- <wsdl:operation name="add">
- <wsdl:input name="add" message="tns:add"> </wsdl:input>
- <wsdl:output name="addResponse" message="tns:addResponse"> </wsdl:output>
- </wsdl:operation>
- </wsdl:portType>
- <wsdl:binding name="PlusServiceSoapBinding" type="tns:Plus">
- <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
- <wsdl:operation name="add">
- <soap:operation style="document" soapAction=""/>
- <wsdl:input name="add">
- <soap:body use="literal"/>
- </wsdl:input>
- <wsdl:output name="addResponse">
- <soap:body use="literal"/>
- </wsdl:output>
- </wsdl:operation>
- </wsdl:binding>
- <wsdl:service name="PlusService">
- <wsdl:port name="PlusPort" binding="tns:PlusServiceSoapBinding">
- <soap:address location="http://127.0.0.1:8888/Plus"/>
- </wsdl:port>
- </wsdl:service>
- </wsdl:definitions>
如果服务端使用ServerFactoryBean类,则最终生成的WSDL文件略有不同
CXF客户端:
如果服务端使用ServerFactoryBean类,则客户端需要使用JaxWsServerFactoryBean类
如果服务端使用JaxWsServerFactoryBean类,则客户端需要使用JaxWsProxyFactoryBean类
- package com.sean.client;
- import org.apache.cxf.frontend.ClientProxyFactoryBean;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- import com.sean.server.Plus;
- public class Client {
- public static void main(String[] args) {
- //ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
- JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
- factory.setAddress("http://127.0.0.1:8888/Plus");
- Plus client = factory.create(Plus.class);
- System.out.println(client.add(2, 2));
- System.exit(0);
- }
- }
无论服务端使用ServerFactoryBean类还是JaxWsServerFactoryBean类,都可在客户端使用JaxWsDynamicClientFactory类,并通过反射的方式调用WebService服务端
- package com.sean.client;
- import org.apache.cxf.endpoint.Client;
- import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
- public class Client2 {
- public static void main(String[] args) throws Exception {
- JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
- Client client = factory.createClient("http://127.0.0.1:8888/Plus?wsdl");
- Object[] inputs = {1, 2};
- Object[] result = client.invoke("add", inputs);
- System.out.println(result[0]);
- }
- }
基于Apache CXF的Web Service服务端/客户端的更多相关文章
- 基于JAX-WS的Web Service服务端/客户端 ;JAX-WS + Spring 开发webservice
一.基于JAX-WS的Web Service服务端/客户端 下面描述的是在main函数中使用JAX-WS的Web Service的方法,不是在web工程里访问,在web工程里访问,参加第二节. JAX ...
- 使用Eclipse自带Web Service插件(Axis1.4)生成Web Service服务端/客户端
创建一个名字为math的Java web工程,并将WSDL文件拷入该工程中 将Axis所需的jar包拷贝至WebRoot\WEB-INF\lib目录下,这些jar包会自动导入math工程中 一,生成W ...
- Eclipse+Axis使用WSDL文件生成Web Service服务端/客户端
JDK版本:1.5.0_22 Eclipse版本:Helios Service Release 2(3.6.2) WSDL文件的创建过程见http://blog.csdn.net/a19881029/ ...
- 使用Eclipse自带的Axis1插件生成Web Service服务端客户端
JDK版本:1.5.0_22 Eclipse版本:Helios Service Release 2(3.6.2) WSDL文件的创建过程见http://blog.csdn.net/a19881029/ ...
- 使用CXF开发Web Service服务
1.使用CXF开发Web Service服务端 1.1 开发一个Web Service业务接口,该接口要用@WebService修饰 (1)创建一个Java项目MyServer (2)在MyServe ...
- Apache CXF实现Web Service(5)—— GZIP使用
Apache CXF实现Web Service(5)-- GZIP使用 参考来源: CXF WebService整合Spring Apache CXF实现Web Service(1)--不借助重量级W ...
- 使用axis开发web service服务端
一.axis环境搭建 1.安装环境 JDK.Tomcat或Resin.eclipse等. 2.到 http://www.apache.org/dyn/closer.cgi/ws/axis/1_4下载A ...
- 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 首先 ...
- Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service
实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http: ...
随机推荐
- C# 线程知识汇总
一.基本概念 进程(process)是windows系统中你的一个基本概念,它包含着一个运行程序所需要的资源.一个正在运行的应用程序在操作系统中被视为一个进程,进程可以包括一个或者多个线程.线程是操作 ...
- unity3d 鼠标事件
using UnityEngine; using System.Collections; public class mouse : MonoBehaviour { //private Vector3 ...
- 给WordPress的TinyMCE Advanced编辑器加上中文字体
为TinyMCE编辑器增加中文字体并不复杂,只需要将将如下代码加到当前主题的functions.php文件中最后一个?>的前面即可: 不要忘记保存,然后更新一遍浏览器即可 //为TinyMCE编 ...
- 使用vs2017创建项目并添加到git中
参考 https://blog.csdn.net/qq373591361/article/details/71194651 https://blog.csdn.net/boonya/article/d ...
- 【Oracle】glogin.sql脚本模板
[root@localhost ~]# su - oracle [oracle@localhost ~]$ vi $ORACLE_HOME/sqlplus/admin/glogin.sql defin ...
- (转)RabbitMQ学习之spring整合发送异步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40919249 实现使用Exchange类型为DirectExchange. routingkey的 ...
- utf8_general_ci、utf8_unicode_ci和utf8_bin的区别(转载)
例如: CREATE DATABASE IF NOT EXISTS redmine DEFAULT CHARSET utf8 COLLATE utf8_general_ci; CREATE DATAB ...
- 当li设置为line-block时,元素之间出现间隙的原因和解决方法
原因 因为浏览器默认把inline元素之间的空白符(Tab.空格.换行)渲染成一个空格.而如下述代码,两个li元素之间的换行符被渲染成一个空格,则元素之间产生了间隙. 用Chrome浏览器将场景模拟出 ...
- N3-1 - 数组 - convert-sorted-array-to-binary-search-tree
题目描述: Given an array where elements are sorted in ascending order, convert it to a height balanced B ...
- Problem 14
Problem 14 # Problem_14.py """ The following iterative sequence is defined for the se ...