使用cxf构建webservice
一、简介
cxf是apache下的一个开源的,功能齐全的WebService框架。是由Celtix和XFire两个开源框架组合而成。cxf可以帮助我们使用前端编程模型构建和开发服务,如JAX-
WS和JAZ-RS。这些服务可以使用不同的协议,如SOAP,XML/HTTP,RESTFul HTTP或者CORBA和使用各种的数据传输协议,如HTTP,JMS 和JBI。除此之外cxf还有如下特性:
- 使用简单,可与maven,spring等进行集成。
- 支持Web Service标准,包括soap,WS-I Basic Profile, WSDL, WS-Addressing, WS-Policy, WS-ReliableMessaging, WS-Security, WS-SecurityPolicy, WS-
- SecureConverstation, 和WS-Trust (partial)。
- 对二进制和遗留协议的支持,cxf设计并提供了一个可插拔的架构,不仅支持xml还支持非xml类型的绑定,如json,corba。可使用多种类型格式进行传输。
- 提供了多中工具生成代码,如WSDL to JAVA, WSDL to JavaScript, Java to JavaScript等。
二、使用
cxf的下载地址如下:http://cxf.apache.org/download.html 。这里下载的是windows版本apache-cxf-3.0.3.zip。在c盘根目录下解压该文件,解压后文件目录结构如下:

其中bin目录下的是为我们提供的相关转换工具,docs目录下的是相关文档,lib目录下是cxf的jar依赖;license目录下的是许可,samples是为我们提供的例子。
新建一个java项目cxf_server,并将lib下的jar文件(不包括endorse,和integration目录)添加到项目的依赖。
1、编写MyService接口 代码如下:
package com.cxf.testauto; import javax.jws.WebService; @WebService
public interface MyService { public String sayHello(String username, String address, String school);
}
2、编写MyService的实现类
package com.cxf.testauto;
public class MyServiceImpl implements MyService {
@Override
public String sayHello(String username, String address, String school) {
System.out.println("sayHello is invoked!");
String result = "hello: " + username + ", your info is: " + address + ", " + school;
return result;
}
}
3、Server 服务的启动类
package com.cxf.testauto; import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class Server { public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setAddress("http://localhost:9000/myservice");
factory.setServiceClass(MyServiceImpl.class);
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.create();
}
}
主要:如果jdk版本比较低会报java.lang.LinkageError: JAXB 2.0 API jar is being loaded, but this RI (...) needs 2.2 API.类似的错误。此时需要在运行该类的时候指定运行参数,打开Run Configurations窗口,在VM arguments输入如下参数:
-Djava.ext.dirs=C:\apache-cxf-3.0.3\lib\endorsed
表示指定jvm运行的时候,加载C:\apache-cxf-3.0.3\lib\endorsed目录下的jar文件。如下图所示

点击Apply,运行即可。
4、运行Server类
启动成功后,我们可以通过浏览器访问wsdl文件,使用http://localhost:9000/myservice?wsdl 地址访问即可。wsdl文件内容如下:
<?xml version='1.0' encoding='UTF-8'?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://testauto.cxf.com/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http"
name="MyServiceImplService" targetNamespace="http://testauto.cxf.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://testauto.cxf.com/" elementFormDefault="unqualified"
targetNamespace="http://testauto.cxf.com/" version="1.0"> <xs:element name="sayHello" type="tns:sayHello" />
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse" /> <xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string" />
<xs:element minOccurs="0" name="arg1" type="xs:string" />
<xs:element minOccurs="0" name="arg2" type="xs:string" />
</xs:sequence>
</xs:complexType> <xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType> </xs:schema>
</wsdl:types> <wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters">
</wsdl:part>
</wsdl:message> <wsdl:portType name="MyService">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType> <wsdl:binding name="MyServiceImplServiceSoapBinding" type="tns:MyService">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document" />
<wsdl:input name="sayHello">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding> <wsdl:service name="MyServiceImplService">
<wsdl:port binding="tns:MyServiceImplServiceSoapBinding"
name="MyServiceImplPort">
<soap:address location="http://localhost:9000/myservice" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
5、通过wsdl文件生成客户端
将C:\apache-cxf-3.0.3\bin 添加进系统环境变量path中。
cmd进入c盘根目录,运行命令
C:\>wsdl2java -client -encoding utf8 -frontend jaxws21 http://localhost:9000/myservice?wsdl
该命令根据wsdl生产调用的客户端。-client表示生成客户端;-encoding表示生成的java代码的编码格式;frontend表示参照jaxws21标准;ttp://localhost:9000/myservice?wsdl为wsdl的地址,也可以是磁盘上的某一个wsdl文件。
执行结果如下:

命令执行成功后将会在c盘根目录下生成客户端的调用代码:生成的代码结构如下

我们主要使用到的就是MyService_MyServiceImplPort_Client.java这个调用类。
6、进行调用,新建另一个java工程cxf_client,将生成的客户端代码拷贝到这个工程中。并对MyService_MyServiceImplPort_Client.java类进行修改。
MyService_MyServiceImplPort_Client.java修改后的内容如下:
package com.cxf.testauto; /**
* Please modify this class to meet your needs
* This class is not complete
*/ import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper; /**
* This class was generated by Apache CXF 3.0.3
* 2015-01-13T13:58:38.847+08:00
* Generated source version: 3.0.3
*
*/
public final class MyService_MyServiceImplPort_Client { private static final QName SERVICE_NAME = new QName("http://testauto.cxf.com/", "MyServiceImplService"); private MyService_MyServiceImplPort_Client() {
} public static void main(String args[]) throws java.lang.Exception {
URL wsdlURL = MyServiceImplService.WSDL_LOCATION;
if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
} MyServiceImplService ss = new MyServiceImplService(wsdlURL, SERVICE_NAME);
MyService port = ss.getMyServiceImplPort(); {
System.out.println("Invoking sayHello...");
java.lang.String _sayHello_arg0 = "zhangsan";
java.lang.String _sayHello_arg1 = "beijing";
java.lang.String _sayHello_arg2 = "hello world";
java.lang.String _sayHello__return = port.sayHello(_sayHello_arg0, _sayHello_arg1, _sayHello_arg2);//对webservice 接口进行调用
System.out.println("sayHello.result=" + _sayHello__return); } System.exit(0);
} }
运行该类,完成调用。
client端打印的结果如下:

server端打印的结果如下:
使用cxf构建webservice的更多相关文章
- cxf构建webservice的两种方式
一.简介 对于基于soap传输协议的webservice有两种开发模式,代码优先和契约优先的模式.代码优先的模式是通过编写服务器端的代码,使用代码生成wsdl:契约优先模式首先编写wsdl,再通过ws ...
- SpringBoot | 第三十四章:CXF构建WebService服务
前言 上一章节,讲解了如何使用Spring-WS构建WebService服务.其实,创建WebService的方式有很多的,今天来看看如何使用apache cxf来构建及调用WebService服务. ...
- Eclipse+Maven+Spring+CXF 构建webservice 服务
一. 软件准备 Eclipse 4.2.1 Maven 2.2.1 Spring 3.2.6 CXF 3.0.2 二. 步骤 首先,在Eclipse中用maven构建一个quickstart版本的ma ...
- 【转】构建基于CXF的WebService服务
构建基于CXF的WebService服务 Apache CXF = Celtix+ XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.C ...
- 3.使用CXF开发webService
CXF 简介 关于 Apache CXF Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache ...
- CXF之webservice
使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...
- Spring Boot+CXF搭建WebService(转)
概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...
- 使用 CXF 做 webservice 简单例子(转载)
使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...
- 使用CXF发布WebService
这里普及一下WebService和cxf的知识.关于webservice和cxf: WebService.各种提供服务的组件 .企业总线.通讯总线(ESB)CXF:是一个SOA框架,Axi ...
随机推荐
- Entity Framework优缺点及使用方法总结
Entity Framework是M$提供的一个ORM框架,它旨在为小型应用程序中数据层的快速开发提供便利. nuget上185W多的下载量,说明.Net开发人员还是比较喜欢用EF的.但是EF在提供了 ...
- [C#高级编程].NET体系结构
本章内容: 编译和运行面向 .NET的代码 MSIL的优点 值类型和引用类型 数据类型化 理解错误处理和特性 程序集..NET基类和命名空间 本章主要介绍一些概念,内容不多. C#是专门为Micros ...
- 查找最小的k 个元素之C#算法实现
紧接着上一篇微软编程面试100题,这次想解决的是查找最小的K个元素,题目是:输入n 个整数,输出其中最小的k 个.例如输入1,2,3,4,5,6,7 和8 这8 个数字,则最小的4 个数字为1,2,3 ...
- SpringMVC——类型转换和格式化、数据校验、客户端显示错误消息
在介绍类型转换和格式化之前,我首先来介绍 <mvc:annotation-driven />. 需要导入的 schema: xmlns:mvc="http://www.sprin ...
- Sprint总结和第八九十的读书笔记
总结:经过这次的Sprint,我在从中收获了很多.作为产品负责人的我,主要责任就是合理分配任务给自己的队友,调动队友的积极性.虽然这其中也有些不尽人意的地方,但是我们都坚持的走了过来,团队合作真的很重 ...
- jQuery点缩略图显示大图片
2015年繁忙的一月份,无更多时间去学习ASP.NET MVC程序,二月份又是中国的新年,长达半个月的假期,望回到老家中,在无电脑无网络的日子里,能有更多时间陪伴年迈的父母亲. 今天学习jQuery的 ...
- js 当前日期及时间
返回时间格式 : 2016-07-22 10:22:30 function getNowFormatDate() { var date = new Date(); var seperator1 = & ...
- 中国快递包裹总量的预测-基于SARIMA模型
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- 再谈visibility:hidden和display:none
之前写过一篇有关visibility:hidden和display:none的文章:为什么要用用visibility:hidden;代替display:none;?主要是从浏览器性能方面入手,却没写两 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q51-Q53)
Question 51You use a third-party site definition to create SharePoint sites.You need to add a Web Pa ...