CXF 教程(一)
CXF Web Service 简单示例
1 准备工作
2 第一个例子
3 客户端
3.1 使用 WSDL 生成客户端
4 RPC 风格
5 相关命令介绍
5.1 Java to WS
1 准备工作
- 从 http://cxf.apache.org 下载 CXF
- 在 Eclipse 中配置 Maven 插件
2 第一个例子
步骤如下
创建 Maven 项目, 使用 quickstart 模板
在 pom.xml 中引入依赖包,如下所示
<properties>
<cxf.version>3.1.0</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>webservices-api</artifactId>
<version>2.0.1</version>
</dependency>
</project>写一个简单的 Service 类,如下所示
package lld.cxf.service;
import javax.jws.WebService;
@WebService
public interface HelloService {
String sayHi(String name);
}package lld.cxf.service;
public class HelloServiceImpl implements HelloService {
public String sayHi(String name) {
return "Hello " + name;
}
}创建 Server 如下所示, 这就是为什么需引入 cxf-rt-transports-http-jetty 包的原因,CXF 内嵌了 Jetty server。
package lld.cxf.service;
import org.apache.cxf.frontend.ServerFactoryBean;
public class HelloServer {
public static void main(String[] args) {
// Create our service implementation
HelloService helloWorldImpl = new HelloServiceImpl();
// Create our Server
ServerFactoryBean svrFactory = new ServerFactoryBean();
svrFactory.setServiceClass(HelloService.class);
svrFactory.setAddress("http://localhost:9000/Hello");
svrFactory.setServiceBean(helloWorldImpl);
svrFactory.create();
}
}运行 Server 后可在浏览器中输入 http://localhost:9000/Hello?wsdl 验证
创建 Client 端如下所示
3 客户端
3.1 使用 WSDL 生成客户端
上例中我们直接把客户端和服务端放在了一个项目中,实际情况一般不会这样。通常是服务端发布 WSDL 的 URL,客户端使用 WSDL 来生成本地 Proxy 代码并访问 Web Service。
首先我们得生成 WSDL 文件,最省事的办法是直接在浏览器中访问上例中的 WSDL 链接并把浏览器中的文本结果另存为本地文件并以 wsdl 作为扩展名
或者我们也可以根据编译结果生成 wsdl 文件。在下载的 CXF 中,在 bin 目录下找到 java2ws 命令,进入结果文件根目录(classes 目录),运行命令如下所示:
java2ws -wsdl -o HelloService.wsdl lld.cxf.service.HelloService
将会在当前目录生成 HelloService.wsdl
根据 wsdl 文件生成客户端 stub,同样是使用 CXF 下载包中的 wsdl2java 命令,如下所示
wsdl2java -client -d ClientDir ../resources/HelloService.wsdl
将把 Stub 生成在当前目录的 ClientDir 目录下
上一步生成的 Stub 中里面包含了很多文件,细节先不用管,把这些文件复制到当前源代码目录中,其中有一个文件 HelloServicePortType_HelloServicePort_Client.java 是一个客户端的调用示例文件,可参考里面的内容写出如下的客户端调用
我将生成的 HelloService.wsdl 文件放在了 resources 目录下,也就是会自动复制到 classes 根目录下。
package lld.cxf.client.test; import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import lld.cxf.service.HelloService;
import lld.cxf.service.HelloServicePortType; public class HelloClientTest {
private static final QName SERVICE_NAME = new QName("http://service.cxf.lld/", "HelloService"); public static void main(String[] args) throws MalformedURLException {
String wsdlFileName = "HelloService.wsdl";
URL wsdlURL = HelloClientTest.class.getClassLoader().getResource(wsdlFileName); HelloService ss = new HelloService(wsdlURL, SERVICE_NAME);
HelloServicePortType port = ss.getHelloServicePort();
System.out.println("Invoking sayHi...");
String result = port.sayHi("Lindong");
System.out.println("sayHi.result=" + result);
}
}另外也可以不必将 wsdl 存放在本地而是直接从远端获取,将上面获取 URL 的代码进行如下替换即可:
String wsdlUrl = "http://localhost:9000/Hello?wsdl";
URL wsdlURL = new URL(wsdlUrl);
通常情况下,如果公司不是按代码量算薪水,我们一般会将 Stub 类打成 jar 包放在引用路径里,以使代码更加清晰。如果使用 Eclipse,可直接使用 Export 功能将选中的 Stub package 导出为jar 包。
4 RPC 风格
将上面的 Service 类修改如下,将生成 RPC 风格的 Web Service
package lld.cxf.service;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public class HelloService {
public String sayHi(String name) {
return "Hello " + name;
}
}
5 相关命令介绍
5.1 Java to WS
5.1.1 Overview
官方帮助:Java to WS
在 2.1 以前的版本中命令为 java2wsdl,在新的版本中为 java2ws
java2ws 用于生成 Web service endpoint's implementation (SEI) 类并根据这些类生成 WSDL 文件, Bean 封装类, 用于启动服务的服务端代码和客户端方问代码。
5.1.2 语法
语法如下所示:
java2ws -databinding -frontend
-wsdl -wrapperbean -client -server -ant -o
-d -classdir
-cp -soap12 -t
-beans *
-address -servicename
-portname -createxsdimports -h -v -verbose
-quiet {classname}
5.1.3 参数说明
各参数说明如下:
|
Option |
Interpretation |
|---|---|
|
|
Displays the online help for this utility and exits. |
|
-o |
Specifies the name of the generated WSDL file. |
|
--databinding |
Specify the data binding (aegis or jaxb). Default is jaxb for jaxws frontend, and aegis for simple frontend. |
|
-frontend |
Specify the frontend to use. jaxws and the simple frontend are supported. |
|
-wsdl |
Specify to generate the WSDL file. |
|
-wrapperbean |
Specify to generate the wrapper and fault bean |
|
-client |
Specify to generate client side code |
|
-server |
Specify to generate server side code |
|
-ant |
Specify to generate an Ant build.xml script |
|
-cp |
Specify the SEI and types class search path of directories and zip/jar files. |
|
-soap12 |
Specifies that the generated WSDL is to include a SOAP 1.2 binding. |
|
-t |
Specifies the target namespace to use in the generated WSDL file. |
|
-servicename |
Specifies the value of the generated service element's name attribute. |
|
-v |
Displays the version number for the tool. |
|
-verbose |
Displays comments during the code generation process. |
|
-quiet |
Suppresses comments during the code generation process. |
|
-s |
The directory in which the generated source files(wrapper bean ,fault bean ,client side or server side code) are placed. |
|
-classdir |
The directory in which the generated sources are compiled into. If not specified, the files are not compiled. |
|
-portname |
Specify the port name to use in the generated wsdl. |
|
-address |
Specify the port address. |
|
-beans |
Specify the pathname of a file defining additional Spring beans to customize databinding configuration. |
|
-createxsdimports |
Output schemas to separate files and use imports to load them instead of inlining them into the wsdl. |
|
-d |
The directory in which the resource files are placed, wsdl file will be placed into this directory by default |
|
classname |
Specifies the name of the SEI class. |
5.1.4 示例
java2ws -wsdl -d ./resources lld.cxf.service.HelloService
java2wsdl -cp ./tmp org.apache.hello_world_soap_http.Greeter
java2wsdl -o hello.wsdl org.apache.hello_world_soap_http.Greeter
java2wsdl -o hello.wsdl -t http://cxf.apache.org org.apache.hello_world_soap_http.Greeter
5.1.5 与 Ant 集成
<?xml version="1.0"?>
<project name="cxf java2ws" basedir=".">
<property name="cxf.home" location ="/usr/myapps/cxf-trunk"/>
<property name="build.classes.dir" location ="${basedir}/build/classes"/>
<path id="cxf.classpath">
<pathelement location="${build.classes.dir}"/>
<fileset dir="${cxf.home}/lib">
<include name="*.jar"/>
</fileset>
</path> <target name="cxfJavaToWS">
<java classname="org.apache.cxf.tools.java2ws.JavaToWS" fork="true">
<arg value="-wsdl"/>
<arg value="-o"/>
<arg value="hello.wsdl"/>
<arg value="service.Greeter"/>
<classpath>
<path refid="cxf.classpath"/>
</classpath>
</java>
</target>
</project>
CXF 教程(一)的更多相关文章
- CXF 教程 (二)
将 Service 布署在远端 1 Overview 2 Server 3 Client 1 Overview 上例中我们的 Server 和 Client 都是在本地.下面演示如果布署在远端需如何修 ...
- Java WebService 教程系列之 Spring 整合 CXF
Java WebService 教程系列之 Spring 整合 CXF 一.引入 jar 包 <dependency> <groupId>org.apache.cxf</ ...
- Apache CXF实现WebService入门教程(附完整源码)
Apache CXF实现WebService非常简单实用,只需要几步就可以实现一个简单的web service. 首先我们需要新建一个maven项目,在pom中添加依赖和jetty作为测试的web s ...
- CXF WebService 教程
业务需求:常见WEB服务: 手机淘宝.京东…. 天气预报 手机号归属地 股票查询 发手机短消息 手机充值功能 中英文翻译 银行转账业务 公司的“进销存系统”在某商品缺货时自动给供应商下订单 ..... ...
- Springmvc集成CXF请看教程二
转自: http://www.cnblogs.com/xiaochangwei/p/5399507.html 继上一篇webService入门之后,http://www.cnblogs.com/xia ...
- 史上最详cxf-Springmvc-maven实现webservice教程(转)
虽知道webservice,工作两年一直没使用过,最近不忙趁机研究了下,实现了简单的服务端及客户端调用.鉴于慕课网没有webservice的教程,大多又都是学生,就在这里跟大家分享下,内容比较详细.大 ...
- CXF实现webservice
虽然网上有很多cxf的教程,但还是要自己写写, “好记性不如烂笔头” 1.服务端 1.1 DEMO,用于测试传递对象 package com.xq.model; import javax.persi ...
- [转贴]JAVA :CXF 简介
Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF.CXF 继 ...
- CXF框架入门实例
首先:什么是CXF?为什么要用CXF? CXF 包含了大量的功能特性,但是主要集中在以下几个方面:支持 Web Services 标准:CXF 支持多种 Web Services 标准,包含 SOAP ...
随机推荐
- (原)Ubuntu连接远程服务器时connection reset by peer
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11086935.html 最近使用ubuntu通过ssh连接服务器时,由于密码错误,多次连接失败后,在 ...
- C#进程间通讯或同步的框架引荐
这篇文章主要介绍了一个进程间通讯同步的C#框架,代码具有相当的稳定性和可维护性,随着.NET的开源也会被注入更多活力,推荐!需要的朋友可以参考下 0.背景简介 微软在 .NET 框架中提供了多种实用 ...
- JDOJ 2157 Increasing
洛谷 P3902 递增 洛谷传送门 JDOJ 2157: Increasing JDOJ传送门 Description 数列A1,A2,--,AN,修改最少的数字,使得数列严格单调递增. Input ...
- 排序算法-希尔排序(Java)
package com.rao.sort; import java.util.Arrays; /** * @author Srao * @className ShellSort * @date 201 ...
- 关于时间排序在ios中失效的处理方法
上个月公司做项目的时候在列表排序的时候产品加了一个需求,通过点击量,发布时间,评论量进行筛选的一个需求. 一开始在电脑上测试基本没问题,然后我也就放下了这个按耐不住的小心脏,然后在完成所有模块后 sh ...
- 【CSP-S膜你考】那23个路口
那23个路口 题面 故事的起源不加赘述,那23个路口. 单刀直入,我直接说题的意思. 蚊子和疯子在做一件事,就是他们要在茫茫的大街上找一个出发点,然后从出发点开始,经过上下左右23次拐弯,到达一个他们 ...
- 11-散列4 Hashing - Hard Version (30 分)
Given a hash table of size N, we can define a hash function H(x)=x%N. Suppose that the linear probin ...
- vue中使用element-ui自定义主题后,vue-cli跑不起来了
环境:vue-cli 2.x版本 自己在官网配置了主题并放到了项目中https://element.eleme.cn/#/zh-CN/theme 然后,我的脚手架在我的电脑中休息了几天,就跑不通了呢! ...
- JAVA JDK安装及path环境变量配置
JDK安装 JVM :JAVA虚拟机 JRE :java运行环境=JVM+核心类库 JDK :JAVA开发工具包=JRE+java开发工具 java开发工具:编译工具(javac.exe) . 运行 ...
- Spring 事物隔离级别,事物传播行为
Spring 框架中对于事物的管理,主要定义了一下四种属性: 事物的隔离(Isolation)级别 事物的传播行为(Propagation Behavior) 事物的超时时间(TImeout) 是否为 ...