So easy Webservice 7.CXF 发布WebService
(一)使用ServerFactoryBean 方式实现发布WS服务
1.新建项目,添加cxf jar包到项目中

2.编写服务实现类
/**
* CXF WebService
* 不用注解
* @author mlxs
*
*/
public class CXFWebService { public String sayHello(String name){
return "hello," + name;
} }
3.编写服务发布类
/**
* CXF 使用ServerFactoryBean发布WS服务,采用编码方式
* 这种方式不好:不支持注解,不能修改WSDL文件
* 如设置服务名称无效:
* @WebService(
* serviceName="cxfHelloService"
* )
* @author mlxs
*
*/
public class CXFPublishWS { public static void main(String[] args) {
String address = "http://127.0.0.1:2345/cxfHello";
ServerFactoryBean factoryBean = new ServerFactoryBean();
//设置服务地址
factoryBean.setAddress(address);
//设置服务实现类
factoryBean.setServiceBean(new CXFWebService());
//发布WS
factoryBean.create(); System.out.println(address + "?WSDL");
}
}
4.访问WSDL地址:http://127.0.0.1:2345/cxfHello?WSDL

5.总结:
这种方式不好:不支持注解,不能修改WSDL文件
* 如设置服务名称无效:
* @WebService(
* serviceName="cxfHelloService"
* )
(二)下面使用支持注解,支持日志的发布方式:
1.创建CXF WS实现类 可以支持注解:自定义服务名
/**
* CXF WebService
* @author mlxs
*
*/
@WebService(
serviceName="CXFWs1"
)
public class CXFWebService { public String sayHello(String name){
return "hello," + name;
}
}
2.创建发布类
/**
* CXF 使用JaxWsServerFactoryBean发布WS服务,采用编码方式
* 这种方式:
* 1.支持注解
* 2.可以打印日志
* @author mlxs
*
*/
public class CXFPublishWS { public static void main(String[] args) {
String address = "http://127.0.0.1:2345/cxfJaxWsHello";
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
//设置服务地址
factoryBean.setAddress(address);
//设置服务实现类
factoryBean.setServiceBean(new CXFWebService());
//支持日志:在有请求进来、和返回给客户端的时候打印日志
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getInInterceptors().add(new LoggingOutInterceptor());
//发布WS
factoryBean.create(); System.out.println(address + "?WSDL");
} }
运行后:
2016-1-27 23:13:37 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://jaxws.cxfws.ws.mlxs.com/}CXFWs1 from class com.mlxs.ws.cxfws.jaxws.CXFWebService
2016-1-27 23:13:37 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://127.0.0.1:2345/cxfJaxWsHello
2016-1-27 23:13:37 org.eclipse.jetty.util.log.Slf4jLog info
信息: jetty-7.4.5.v20110725
2016-1-27 23:13:37 org.eclipse.jetty.util.log.Slf4jLog info
信息: Started SelectChannelConnector@127.0.0.1:2345 STARTING
2016-1-27 23:13:37 org.eclipse.jetty.util.log.Slf4jLog info
信息: started o.e.j.s.h.ContextHandler{,null}
http://127.0.0.1:2345/cxfJaxWsHello?WSDL
3.访问WSDL地址(Get请求):

控制台日志:
2016-1-27 23:16:30 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
信息: Inbound Message
----------------------------
ID: 1
Address: http://127.0.0.1:2345/cxfJaxWsHello?WSDL
Http-Method: GET
Content-Type:
Headers: {Accept=[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8], accept-encoding=[gzip, deflate, sdch], Accept-Language=[zh-CN,zh;q=0.8], Cache-Control=[max-age=0], connection=[keep-alive], Content-Type=[null], Host=[127.0.0.1:2345], Upgrade-Insecure-Requests=[1], User-Agent=[Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36]}
--------------------------------------
4.使用wsimport下载源码,放到client工程中,执行访问WS接口:
public static void main(String[] args) {
CXFWs1 ws = new CXFWs1();
CXFWebService port = ws.getCXFWebServicePort();
System.out.println(port.sayHello("administrator"));
}
客户端输出:
hello,administrator
服务端日志:
2016-1-27 23:21:41 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
信息: Inbound Message
----------------------------
ID: 4
Address: http://127.0.0.1:2345/cxfJaxWsHello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2], connection=[keep-alive], Content-Length=[215], content-type=[text/xml; charset=UTF-8], Host=[127.0.0.1:2345], SOAPAction=[""], User-Agent=[Java/1.6.0_13]}
Payload: <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHello xmlns:ns2="http://jaxws.cxfws.ws.mlxs.com/"><arg0>administrator</arg0></ns2:sayHello></S:Body></S:Envelope>
--------------------------------------
(三)WebService总结
1.ws访问流程: 在调用方法的时候先发送一条get请求去访问远程的wsdl文件(此文件中有相应的公共接口和可以调用的方法),此流程称为"握手"
2.然后在客户端发送 post请求传输 soap数据交给服务器,最后服务器返回soap格式给客户端
3.前面所讲的ws服务都是硬编码的服务, Service应该是单例模式, 如果有Spring,CXF应该需要交给Spring管理
So easy Webservice 7.CXF 发布WebService的更多相关文章
- Spring集成CXF发布WebService并在客户端调用
Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...
- 使用CXF发布WebService
这里普及一下WebService和cxf的知识.关于webservice和cxf: WebService.各种提供服务的组件 .企业总线.通讯总线(ESB)CXF:是一个SOA框架,Axi ...
- CXF发布webService服务以及客户端调用
这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...
- SpringBoot整合cxf发布webService
1. 看看项目结构图 2. cxf的pom依赖 1 <dependency>2 <groupId>org.apache.cxf</groupId>3 <art ...
- SpringMVC4整合CXF发布WebService
SpringMVC4整合CXF发布WebService版本:SpringMVC 4.1.6,CXF 3.1.0项目管理:apache-maven-3.3.3 pom.xml <project x ...
- 使用CXF发布WebService服务简单实例
一.说明: 前面介绍了使用axis2来发布Webservice服务,现在介绍一种更popular,更高效的Webservice服务发布技术:CXF Apache CXF = Celtix + XFir ...
- CXF发布webservice入门
1.设置CXF的bin目录进环境变量 2.CXF导入相关的jar包. 3.建立接口 @WebService public interface HelloWorld { public void say( ...
- [置顶] 利用CXF发布webService的小demo
其实webService的发布不仅仅只有xfire,今天,给大家介绍一下用CXF发布一个webService的小demo,CXF也是我做webService用的第一个框架... 先将相关的jar引进来 ...
- Spring+CXF整合来管理webservice(服务器启动发布webservice)
Spring+CXF整合来管理webservice 实现步骤: 1. 添加cxf.jar 包(集成了Spring.jar.servlet.jar ),spring.jar包 ,serv ...
随机推荐
- 查找(AVL平衡二叉树)
[1]为什么需要平衡二叉树? 矛盾是推进事物向前发展的源动力. 那么平衡二叉树是从哪里来?肯定是有矛盾存在的.请看程来师的分析: [2]什么是平衡二叉树? 平衡二叉树的基本认识: [3]平衡二叉树的构 ...
- FireDac 与数据库连接时字符集及对应的字段类型问题
近日在一个过程调用时发生一个奇怪现象, 异常返回意思是说, 数据的长度是[6], 而字段定义的长度是[3]. 分析后认为: 调用过程你不涉及到对返回数据集的字段手动定义问题, 出现这个问题应是两边 ...
- 161118、linux(centos) 项目部署阶段相关命令汇总
1.ssh免密码登陆 主要命令 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys-->添加公钥 service sshd restart ...
- https笔记
TCP提供了可靠的,面向连接的字节流服务. 1)应用数据分割成TCP认为适合发送的数据块,通过MSS(最大数据包长度)来控制. 2)重传机制 3)对首部和数据进行校验 4)TCP对收到的数据进行排序, ...
- rotate array 旋转数组
class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); int i=0 ...
- IOS 音频开发文件大小计算
音频基础知识 音频文件计算大小 音频转码 标签(空格分隔): 调查 IOS音频 https://developer.apple.com/library/ios/documentation/MusicA ...
- 面向生产环境的大集群模式安装Hadoop
一.实验说明 1.本实验将使用DNS而不是hosts文件解析主机名: 2.使用NFS共享密钥文件,而不是逐个手工拷贝添加密钥: 3.复制Hadoop时使用批量拷贝脚本而不是逐台复制. 测试环境: Ho ...
- MongoDB在windows自启动
D:\mongodb\Server\3.0\bin>mongod --logpath D:\mongodb\log\mongo.log --logappend--dbpath D:\mongod ...
- 2013 Asia Regional Changchun I 题,HDU(4821),Hash
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比 ...
- Eclipse 中outline的小图标的含义(zend也一样)
颜色:绿色:public黄色:protected蓝色:no modifier红色:private形状:实心:method空心:variable实心中间有字母C:classClass右侧有向右的箭头:运 ...