使用cxf开发webservice接口
项目中经常用到开发webservice接口,及调用webService接口。这里讲解如何使用cxf开发webService接口。
一、webservice介绍及理解
webservice是一种跨平台,跨语言的规范,用于不同平台,不同语言开发的应用之间的交互。
比如,平台平台淘宝、京东想获取其他快递公司数据接口,需快递公司开放数据接口。
那么 webservice就是出于以上类似需求而定义出来的规范;无需关心对方什么平台上开发以及使用何种语言开发。
只关心调用对方发布webservice接口的一些对我们获取有用数据的方法。
开发人员一般就是在具体平台开发webservice接口,以及调用webservice接口;每种开发语言都有自己的webservice实现框架。
比如Java 就有 Apache Axis1、Apache Axis2、Codehaus XFire、Apache CXF、Apache Wink、Jboss RESTEasyd等等...

二、cxf
cxf是java开发webService的一种实现框架技术。目前,cxf是主流的webService实现框架。
使用cxf开发需引入cxf开发相关jar包,maven项目中pom.xml配置如下:

1 <!--添加cxf支持 -->
2 <dependency>
3 <groupId>org.apache.cxf</groupId>
4 <artifactId>cxf-rt-frontend-jaxws</artifactId>
5 <version>3.1.9</version>
6 </dependency>
7 <dependency>
8 <groupId>org.apache.cxf</groupId>
9 <artifactId>cxf-rt-transports-http-jetty</artifactId>
10 <version>3.1.9</version>
11 </dependency>
12 <dependency>
13 <groupId>org.apache.cxf</groupId>
14 <artifactId>cxf-core</artifactId>
15 <version>3.1.9</version>
16 </dependency>

备注:这里要额外加入jetty,作为webservice发布的服务器。jetty是一个内嵌的web服务器;
使用JaxWsServerFactoryBean类创建工厂设置暴露地址、接口类、接口实现类,创建即可发布。
三、下面演示其实现过程
发布webService接口,需一个发布服务的url地址,及对应的接口。Jdk自身有实现WebService。
具体实现代码如下:
根据规范,我们先建一个接口类:HelloWorld

1 /**
2 *
3 */
4 package com.hik.webservice;
5
6 import javax.jws.WebService;
7
8 /**
9 * @ClassName: HelloWorld
10 * @Description: TODO
11 * @author jed
12 * @date 2017年7月30日上午10:20:35
13 *
14 */
15 @WebService
16 public interface HelloWorld {
17
18 public String say(String str);
19 }

再建一个具体的实现类:HelloWorldImpl

1 /**
2 *
3 */
4 package com.hik.webservice.impl;
5
6 import javax.jws.WebService;
7
8 import com.hik.webservice.HelloWorld;
9
10 /**
11 * @ClassName: HelloWorldImpl
12 * @Description: TODO
13 * @author jed
14 * @date 2017年7月30日上午10:24:46
15 *
16 */
17 @WebService
18 public class HelloWorldImpl implements HelloWorld{
19
20 public String say(String str) {
21 return "hello"+str;
22 }
23
24 }

最后建一个发布服务的主类:Server

1 /**
2 *
3 */
4 package com.hik.webservice;
5
6 import javax.xml.ws.Endpoint;
7
8
9 import com.hik.webservice.impl.HelloWorldImpl;
10
11 /**
12 * @ClassName: Server
13 * @Description: TODO
14 * @author jed
15 * @date 2017年7月30日上午10:26:16
16 *
17 */
18 public class Server {
19
20 public static void main(String[] args) {
21 System.out.println("web Service start");
22 HelloWorldImpl implementor = new HelloWorldImpl();
23 String address="http://192.168.0.102/helloWorld";
24 Endpoint.publish(address, implementor);//JDK实现
25 System.out.println("web Service started");
26
27 }
28 }

这里的Endpoint是Jdk自身实现的WebService。这里的address,写上自己的本机IP
我们运行下Server类:
运行效果如下:

我们在浏览器里访问:http://192.168.1.102/helloWorld?wsdl
效果:

说明已经成功调用了webservice接口;
这里的wsdl 是 Web Services Description Language的缩写,是一个用来描述Web服务和说明如何与Web服务通信的XML语言。WSDL是Web Service的描述语言,用于描述Web Service的服务,接口绑定等,为用户提供详细的接口说明书。
请求后得到的是一个xml规范文档。是一套规范,任何语言平台技术都可以解析。
CXF来实现webservice接口
我们把Server改下。换成CXF实现:

1 /**
2 *
3 */
4 package com.hik.webservice;
5
6 import javax.xml.ws.Endpoint;
7
8 import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
9
10 import com.hik.webservice.impl.HelloWorldImpl;
11
12 /**
13 * @ClassName: Server
14 * @Description: TODO
15 * @author jed
16 * @date 2017年7月30日上午10:26:16
17 *
18 */
19 public class Server {
20
21 public static void main(String[] args) {
22 System.out.println("web Service start");
23 HelloWorldImpl implementor = new HelloWorldImpl();
24 String address="http://192.168.0.102/helloWorld";
25 //Endpoint.publish(address, implementor);//JDK实现
26 JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
27 factoryBean.setAddress(address); //设置暴露地址
28 factoryBean.setServiceClass(HelloWorld.class); //接口类
29 factoryBean.setServiceBean(implementor); //设置实现类
30 factoryBean.create();
31 System.out.println("web Service started");
32
33 }
34 }

效果和jdk实现的一样

from: https://www.cnblogs.com/jedjia/p/cxf.html
使用cxf开发webservice接口的更多相关文章
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- 【WebService】使用CXF开发WebService(四)
CXF简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix ...
- 使用cxf开发webservice应用时抛出异常
在使用cxf开发webservice应用时,报出了类似下面的错误 JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:& ...
- (二)使用CXF开发WebService服务器端接口
CXF作为java领域主流的WebService实现框架,Java程序员有必要掌握它. CXF主页:http://cxf.apache.org/ 简介:百度百科 今天的话,主要是用CXF来开发下Web ...
- 3.使用CXF开发webService
CXF 简介 关于 Apache CXF Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache ...
- [转] WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单
以下文章来自 http://www.blogjava.net/jacally/articles/186655.html 现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了 ...
- Spring boot+CXF开发WebService
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- Spring boot+CXF开发WebService Demo
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- (三)使用CXF开发WebService客户端
前面一讲开发了webservice服务器端接口,今天的话,我们来开发webservice客户端,让大家来体验下过程: 首先建一个Maven项目,项目名字,WS_Client: 然后我们要用CXF给我们 ...
随机推荐
- windows7无声音,提示未插入扬声器或耳机的解决
windows7无声音,提示未插入扬声器或耳机的解决: http://jingyan.baidu.com/article/358570f6043a85ce4624fc47.html
- 在Linux下将TPC-H数据导入到MySQL
一.下载TPC-H 下载地址:http://www.tpc.org/tpc_documents_current_versions/current_specifications.asp .从这个页面中找 ...
- 《Android源码设计模式》--策略模式
No1: 定义:策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化. No2: 使用场景: 1)针对同一类型问题的多种处理方式,仅 ...
- 洛谷P4623 [COCI2012-2013#6] BUREK [模拟]
题目传送门 BUREK 格式难调,题面就不放了. 分析: 一道比较有思维难度的模拟题. 首先我们可以想到,对于一个三角形,可以画出一个最小矩形使得这个三角形被完全包围,并且这个矩形的边平行于坐标轴(图 ...
- CentOS通过光盘启动救援数据
(1).CentOS6 1)首先确保实体机有光盘,虚拟机有光盘镜像.并通过BIOS设置从光盘启动,实体机请通过提示进入BIOS,虚拟机请找到上方菜单中虚拟机-->电源-->打开电源时进入固 ...
- [leetcode greedy]45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 1007 Maximum Subsequence Sum (25)(25 point(s))
problem Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is define ...
- ubuntu python opencv3 cv2.cv2 has no attribute 'face' 'cv2.face' has no attribute 'createEigenFaceRecognizer'
学习opencv过程中遇到错误: 1 cv2.cv2 has no attribute 'face' 经过一顿查,,,各种走弯路 最后一下子就解决了: pip install opencv-pyth ...
- 排序算法之直接插入排序Java实现
排序算法之直接插入排序 舞蹈演示排序: 冒泡排序: http://t.cn/hrf58M 希尔排序:http://t.cn/hrosvb 选择排序:http://t.cn/hros6e 插入排序: ...
- LOJ P1155 双栈排序 二分图染色 图论
https://www.luogu.org/problem/show?pid=P1155 题解: https://www.byvoid.com/zhs/blog/noip2008-twostack 开 ...