基于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: ...
随机推荐
- ADO.NET改进防注入
static void Main1(string[] args) { //用户输入一个需要查询的条件 car表 Console.WriteLine("请输入"); string c ...
- mybatis 高级映射和spring整合之查询缓存(5)
mybatis 高级映射和spring整合之查询缓存(5) 2.0 查询缓存 2.0.1 什么是查询缓存 mybatis提供缓存,用于减轻数据压力,提高数据库性能. mybatis提供一级缓存和二级缓 ...
- LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- 查询 MySQL 库/表相关信息
SHOW DATABASES //列出 MySQL Server 数据库. SHOW TABLES [FROM db_name] //列出数据库数据表. SHOW CREATE TABLES tbl_ ...
- iOS11关于隐藏导航栏后带有tableView界面出现,下移问题
//解决iOS11关于隐藏导航栏后带有scrollView界面出现,下移问题 if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdj ...
- 3D集合图元:最小边界框/包围盒(boundingbox)
对于2D边界框的应用时比较广泛地,它为一个简单匹配建立了很小的计算规则,3D模型的boundingbox则比较困难,计算代价较大.对于PCL库的使用则降低了计算难度,三维数值化降低了建模过程,可以使用 ...
- QT线程使用收集示例
关于多线程问题: Qt和Boost做跨平台的线程封装,OpenMP主要做并行计算,让不精通多线程的人也能高效地利用CPU的计算能力.个人倾向于用boost.thread, boost.mpi. 一 ...
- Find Bugs
为什么没有早点知道有这么好用的插件呢?
- 详解优动漫PAINT中的图层模式
使用优动漫PAINT绘制漫画或者插画的时候,在其新建画布区域有一个基本颜色模式的选项,分别包括彩色模式.灰度模式和黑白位图模式,那么这三个模式有什么区别呢,我们在绘图的时候应该如何选择呢? 彩色模式: ...
- Linux date命令的用法(转)
1.命令:date 2.命令功能:date 可以用来显示或设定系统的日期与时间. 3.命令参数 -d<字符串>:显示字符串所指的日期与时间.字符串前后必须加上双引号: -s<字符串& ...