【WebService】使用CXF开发WebService(四)
CXF简介
Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。
使用CXF开发WebService服务端
1、使用eclipse新建一个服务端java工程

2、使用CXF开发WebService服务端需要导入相应的jar包,所以这一步先下载cxf的jar包,官网:http://cxf.apache.org/

解压下载的cxf文件如下:

3、在解压后的lib目录中,有很多cxf相关的jar包,有些jar包不一定要使用,本例选出一些必要的jar包,拷贝到java工程里面(当然也可以拷贝lib目录下的所有jar包)。
在工程下新建lib目录,并把jar包拷贝到此目录,同时添加引用关系。如下图:

4、新建一个接口,使用@WebService(SEI和SEI的实现类)注解标注接口,使用@WebMethod(SEI中的所有方法)注解标注接口中定义的所有方法,如下:
package com.test.ws; import javax.jws.WebMethod;
import javax.jws.WebService; /**
* 定义SEI(WebService EndPoint Interface)终端
* @author H__D
* @date 2017年7月28日 上午11:35:34
*
*/
//使用@WebService注解标注WebServiceI接口
@WebService
public interface HelloWS { //使用@WebMethod注解标注WebServiceI接口中的方法
@WebMethod
public String sayHello(String name); }
5、编写一个接口实现类,使用@WebService注解标注实现类,如下:
package com.test.ws; import javax.jws.WebService; /**
* SEI的具体实现
* @author H__D
* @date 2017年7月28日 上午11:37:43
*
*/
//使用@WebService注解标注
@WebService
public class HelloWSImpl implements HelloWS{ @Override
public String sayHello(String name) {
System.out.println("WebService sayHello : " + name);
return "Hello : " + name;
}
}
6、使用Endpoint(终端)类发布webservice,如下:
package com.test.ws.server; import javax.xml.ws.Endpoint; import com.test.ws.HelloWSImpl; /**
* 发布Web Service
* @author H__D
* @date 2017年7月28日 上午11:40:48
*
*/
public class ServerTest { public static void main(String[] args) { //定义WebService的发布地址,这个地址就是提供给外界访问Webervice的URL地址,URL地址格式为:http://ip:端口号/xxxx
String address = "http://127.0.0.1:8989/test-webservice/hellows";
//使用Endpoint类提供的publish方法发布WebService,发布时要保证使用的端口号没有被其他应用程序占用
Endpoint.publish(address, new HelloWSImpl());
System.out.println("发布webservice成功!"); }
}
7、运行SeverTest类的main方法,使用浏览器进行访问,访问地址:http://127.0.0.1:8989/test-webservice/hellows,如下:
控制台输出:

使用CXF开发WebService客户端
1、使用eclipse新建一个客服端java工程

2、使用cxf包中的wsdl2java.bat工具(在cxf包中的bin目录中)生成客户端代码,如下:

执行命令:wsdl2java -d location(输入目录,生成.java文件会在该目录) -all url(url为wsdl文件的路径)
参数说明:
-p 也就是package 对应Java中的包
-d 输入目录,生成.java文件会在该目录,会自动添加-p参数配置的包路径
-client 生成客户端测试web service的代码.
-server 生成服务器启动web service的代码.
-impl 生成web service的实现代码.
-ant 生成build.xml文件.
-all 生成上面-client -server -impl -ant 对应的所有文件.
打开cmd命令窗口--》进入cxf包中的bin目录--》执行命令。如下:

刷新工程即可看到生成的代码。

3、编写调用WebService对外提供的方法,如下:
package com.test.ws.client; import com.test.ws.HelloWS;
import com.test.ws.HelloWSImpl;
import com.test.ws.HelloWSImplService; /**
* 调用WebService的客户端
* @author H__D
* @date 2017年7月28日 下午2:39:24
*
*/
public class WSClient { public static void main(String[] args) {
//创建一个用于产生WebServiceImpl实例的工厂,WebServiceImplService类是wsimport工具生成的
HelloWSImplService factory = new HelloWSImplService();
//通过工厂生成一个WebServiceImpl实例,WebServiceImpl是wsimport工具生成的
HelloWS helloWS = factory.getHelloWSImplPort();
System.out.println(helloWS.getClass()); //调用WebService的sayHello方法
String result = helloWS.sayHello("Jack");
System.out.println(result);
} }
4、控制台输出,如下:

【WebService】使用CXF开发WebService(四)的更多相关文章
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- 使用cxf开发webservice应用时抛出异常
在使用cxf开发webservice应用时,报出了类似下面的错误 JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:& ...
- 使用cxf开发webservice接口
项目中经常用到开发webservice接口,及调用webService接口.这里讲解如何使用cxf开发webService接口. 一.webservice介绍及理解 webservice是一种跨平台, ...
- 3.使用CXF开发webService
CXF 简介 关于 Apache CXF Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache ...
- Spring boot+CXF开发WebService
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- Spring boot+CXF开发WebService Demo
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- [转] WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单
以下文章来自 http://www.blogjava.net/jacally/articles/186655.html 现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了 ...
- (三)使用CXF开发WebService客户端
前面一讲开发了webservice服务器端接口,今天的话,我们来开发webservice客户端,让大家来体验下过程: 首先建一个Maven项目,项目名字,WS_Client: 然后我们要用CXF给我们 ...
- (二)使用CXF开发WebService服务器端接口
CXF作为java领域主流的WebService实现框架,Java程序员有必要掌握它. CXF主页:http://cxf.apache.org/ 简介:百度百科 今天的话,主要是用CXF来开发下Web ...
随机推荐
- 织梦 百度sitemap制作教程
一.新建一个sitemap.htm模板 登录dedecms后台,选择[模板]-[模板管理]-[默认模板管理] 点击最下面的[新建模板]新建一个模板,并复制下面这段代码进去(将代码中的域名改为自己的): ...
- 二:python 对象类型概述
1,为什么使用内置类型: a)内置对象使程序更容易编写 b)内置对象是扩展的组件 c)内置对象往往比定制的数据结构更加高效 d)内置对象是语言的标准的一部分 2,python 的主要内置对象 对象类 ...
- DC-学习
1.DC概论一:setup time, hold time之一 http://www.blogbus.com/bb2hh-logs/20463915.html 2.DC概论二:fanout, skew ...
- 33. Search in Rotated Sorted Array (Array;Divide-and-Conquer)
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 《学习OpenCV(中文版)》
<模式识别中文版(希)西奥多里蒂斯> <学习OpenCV(中文版)> 矩阵计算 英文版 第四版 Matrix Computations OpenCV 3.x with Pyth ...
- 100-days: Five
Title: Feel better now ? The rise and rise of the anxiety economy(焦虑经济) rise and rise 一直上升 anxiety n ...
- ALV界面显示
PERFORM ADD. IS_LAYOUT_LVC-CWIDTH_OPT = 'X'. IS_LAYOUT_LVC-SEL_MODE = 'A'. CALL FUNCTION 'REUSE_ ...
- stark组件开发之URL别名的设置
from django.urls import re_path from stark.servers.start_v1 import site, StartHandler from django.ht ...
- 开启Windows文件共享必须开启的两个服务
开启Windows文件共享必须开启的两个服务分别是: 1.Server服务. 2.Workstation服务. 如果文件共享丢失,可以去首先去查看这两个服务是否停止,如果停止,请启动或者重启.
- C#设计模式-2工厂方法模式(Factory Method)
什么是工厂模式?类比生活中的概念,当我们需要打电话的时候,我们需要一部手机,我们通常会选择直接去卖手机的实体店买.但在程序设计中,当我们需要调用一个类(PhoneA或PhoneB)的方法的时候,我们往 ...