项目中经常用到开发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接口的更多相关文章

  1. struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

    Cxf + Spring+ myeclipse+ cxf 进行  Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...

  2. 【WebService】使用CXF开发WebService(四)

    CXF简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix ...

  3. 使用cxf开发webservice应用时抛出异常

    在使用cxf开发webservice应用时,报出了类似下面的错误 JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:& ...

  4. (二)使用CXF开发WebService服务器端接口

    CXF作为java领域主流的WebService实现框架,Java程序员有必要掌握它. CXF主页:http://cxf.apache.org/ 简介:百度百科 今天的话,主要是用CXF来开发下Web ...

  5. 3.使用CXF开发webService

    CXF 简介 关于 Apache CXF Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache ...

  6. [转] WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单

    以下文章来自   http://www.blogjava.net/jacally/articles/186655.html 现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了 ...

  7. Spring boot+CXF开发WebService

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...

  8. Spring boot+CXF开发WebService Demo

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...

  9. (三)使用CXF开发WebService客户端

    前面一讲开发了webservice服务器端接口,今天的话,我们来开发webservice客户端,让大家来体验下过程: 首先建一个Maven项目,项目名字,WS_Client: 然后我们要用CXF给我们 ...

随机推荐

  1. 【51nod】1061 最复杂的数 V2

    题解 我是榜上最后一名= = 可能高精度用vector太慢了吧--什么破题= = 这道题很简单,如果高精度熟练代码--也很简单--然而,参数调了好久 我们发现质数的指数一定是,质数越小,指数越大,这个 ...

  2. pct_free

    SQL> select table_name,pct_free,pct_used from user_tables; TABLE_NAME PCT_FREE PCT_USED---------- ...

  3. PhantomJS 远程做调试

    做爬虫的工程师,一定会用到phantomjs,这是一个在linux上用的无界面的浏览器 在终端用phantomjs来爬取数据,或者是做测试,怎么去能看到执行到哪一步了,去实时的观测. 其实chrome ...

  4. javacript 实现两个数组的差集

    <script type="text/javascript">      var array1 = [1,2,3,4,5,6,7,8,9];      var arra ...

  5. http远程调用原生get、post模板

    一.get方法 package lq.httpclient.method; import java.io.BufferedReader; import java.io.IOException; imp ...

  6. 再谈应用环境下的TIME_WAIT和CLOSE_WAIT

    昨天解决了一个HttpClient调用错误导致的服务器异常,具体过程如下: http://blog.csdn.net/shootyou/article/details/6615051 里头的分析过程有 ...

  7. python opencv3 运动检测

    git:https://github.com/linyi0604/Computer-Vision 思路:  开启摄像头后 设置一个当前帧为背景, 在之后检测到的帧都与背景对比不同,对不同的地方进行检测 ...

  8. [OpenGL]纹理贴图实现 总结

    实现步骤 第一步:设置所需要的OpenGL环境 设置上下文环境 删除已经存在的渲染的缓存 设置颜色缓存 设置帧缓存 清除缓存 设置窗口大小 开启功能 编译shander 使用program 获取sha ...

  9. PYQT控件使用

    QtGui.QComboBox .addItem(string)#添加字符串项到Item.addItems(list)#添加列表或元组元素到Item.clear()#清除所有Item.clearEdi ...

  10. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集

    D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...