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 ...
随机推荐
- 【python cookbook】【字符串与文本】3.利用shell通配符做字符串匹配
问题:当工作在Linux shell下时,使用常见的通配符模式(即,*.py.Dat[0-9]*.csv等)来对文本做匹配 解决方案:fnmatch模块提供的两个函数fnmatch().fnmatch ...
- svn使用相关问题:eclipse插件,加锁,解锁,偷锁,更新不了,记住密码
svn使用相关问题:eclipse插件,加锁,解锁,偷锁,更新不了,记住密码 获取锁的时候可以看下 是谁锁住了,让对方提交解锁,如果是给离职人员锁住需要使用偷锁的方式先解锁再提交偷锁处理办法:选中该文 ...
- web.xml中 error-page的正确用法
<error-page> <error-code>404</error-code> <location>/mvc/hello1?i=1</loca ...
- 判断listview是上滑还是下滑的方法
方法一: 用setOnScrollListener(new AbsListView.OnScrollListener())来实现,判断滑动后显示的第一个条目 ,与滑动前的第一个条目的大小来判断, 这种 ...
- css不同浏览器兼容性调试 --- 转自: [http://wo.115.com/?ct=detail&id=31733&bid=1018841]
css不同浏览器兼容性调试 IE6.0,IE7.0与Firefox的CSS兼容性问题1.DOCTYPE 影响 CSS 处理 2.FF: div 设置 margin-left, margin-right ...
- 我的CSS样式记事本(1)
文本 行高: line-height 对齐方式: text-align 字符间距: letter-spacing 文本修饰: text-decoration字体 设置字体所有: font 字体类型: ...
- easyui的页面等待提示层,即mask
/* * 使用方法: * 开启:MaskUtil.mask(); * 关闭:MaskUtil.unmask(); * * MaskUtil.mask('其它提示文字...'); */ var Mask ...
- js判断radio,checkbox是否选中
从数据库循环数据,多选按钮数组 function type_1(){ //多选 var b= document.getElementsByName('service_zj_ids[]'); var ...
- JQuery知识快览之四—样式
前面我们获取了对象集,本文介绍怎么控制对象集的样式 基本概念 在一个html页面中,我们有两种方式来控制一个对象的样式,用HTML attribute控制,或者用CSS类来控制,这两种方法虽然都能控制 ...
- 第十二届浙江省大学生程序设计大赛-Demacia of the Ancients 分类: 比赛 2015-06-26 14:39 30人阅读 评论(0) 收藏
Demacia of the Ancients Time Limit: 2 Seconds Memory Limit: 65536 KB There is a popular multiplayer ...