基于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: ...
随机推荐
- 在django中应用装饰器(一)
在新写的博客应用中,涉及很多关于权限的问题,比如修改用户信息,博客的修改与删除,虽然默认的提交信息都是session的用户,但是也应该防止一下篡改提交的可能,之前想的是在每个view中加一段判断的逻辑 ...
- JavaScript定时器的开启关闭
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- npm一点点
写在开头 要抓紧学习了,不然要遭... 月底之前有大量东西要学习,干 npm 包管理工具 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并安装别人编写的命令行程序 ...
- winserver2012安装.net 3.5
运行 dism.exe /online /enable-feature /featurename:NetFX3 /Source:I:\sources\sxs
- jdbc转账操作
public class cs{ public static void main(String[] args){ try{ Connection conn=JdbcUtils.getConnectio ...
- Asp.net Core 源码-SessionExtensions
using Microsoft.AspNetCore.Http; using Newtonsoft.Json; namespace SportsStore.Infrastructure { publi ...
- jmeter录制
1.添加线程组 2.添加HTTP代理服务 3.浏览器的代理设置 4.添加证书 5.排除模式 .*\.(jpg|css|png|git).*或者 .*\.jpg 6.录制只限制某一个ip段 7.最后一句 ...
- java:递归算法
JAVA中的递归是只一个方法在(满足条件时(或不满足条件时[这里的判断根据业务的实际需求写]))自己调用自己的方法名,要求参数和方法名一致, 然后根据判断跳出该方法,返回相应的返回值! 实例: 我们要 ...
- 2019-04-03 SQL Group By某列,预先对该列进行一个预处理,提炼出共有的信息,即关键字case when 列名什么条件 then 赋值 else 赋值 end as 新列名
select sum(发行金额) from( select PoolNameFormat,count(cast(ItemValue as decimal(19,4))) as 发行笔数,sum(cas ...
- STM32 ADC多通道转换DMA模式与非DMA模式两种方法(HAL库)
一.非DMA模式(转) 说明:这个是自己刚做的时候百度出来的,不是我自己做出来的,因为感觉有用就保存下来做学习用,原文链接:https://blog.csdn.net/qq_24815615/arti ...