使用CXF实现WebService
一、首先创建一个maven项目,引入相应的jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>xw</groupId>
<artifactId>xw</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<cxf.version>2.2.3</cxf.version>
</properties> <dependencies>
<!-- CXF 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>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency> </dependencies> <build>
<finalName>spirngMVC</finalName>
<plugins>
<!-- 编码和编译和JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、服务端
1、公开Web服务的接口IHelloService
@WebService
public interface IHelloService { Customer selectMaxIdCustomer(Customer c1,Customer c2); Customer selectLongNameCustomer(Customer c1,Customer c2);
}
使用@WebService注解表示公开为Web服务,使用这个注解的接口将公开所有的方法,如果你想屏蔽某个方法,可以使用方法注解@WebMethod(exclude = true)设置为true表示屏蔽该方法。
2、实现类HelloServiceImpl
public class HelloServiceImpl implements IHelloService {
@Override
public Customer selectMaxIdCustomer(Customer c1, Customer c2) {
if(c1.getId() >= c2.getId() ){
return c1;
}else{
return c2;
}
}
@Override
public Customer selectLongNameCustomer(Customer c1, Customer c2) {
if(c1.getName().length() >= c2.getName().length() ){
return c1;
}else{
return c2;
}
}
}
实现了Web服务的具体功能。
3、Customer类
@XmlRootElement(name = "Customer")
public class Customer {
private long id;
private String name; public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
这个类是公开为Web服务的接口中的参数类型和返回值,因此你需要使用@XmlRootElement(name = "Customer")注解告诉CXF如何在XML和Java Object之间处理。
4、发布Web服务
public class ServerSimple {
public static void main(String[] args) throws Exception{
Endpoint.publish("http://localhost:8080/helloWorld", new HelloServiceImpl());
}
}
使用CXF自带的Jetty运行服务端,设置URL和服务类就行
三、查看WSDL
访问http://localhost:8080/helloWorld?wsdl地址可以看到很长的XML,就是WSDL,如果看到WSDL就代表Web服务发布成功了
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://cxf/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloServiceImplService" targetNamespace="http://cxf/">
<wsdl:types>
<xs:schema xmlns:tns="http://cxf/" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://cxf/" version="1.0">
<xs:element name="Customer" type="tns:customer"/>
<xs:element name="selectLongNameCustomer" type="tns:selectLongNameCustomer"/>
<xs:element name="selectLongNameCustomerResponse" type="tns:selectLongNameCustomerResponse"/>
<xs:element name="selectMaxIdCustomer" type="tns:selectMaxIdCustomer"/>
<xs:element name="selectMaxIdCustomerResponse" type="tns:selectMaxIdCustomerResponse"/>
<xs:complexType name="selectLongNameCustomer">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:customer"/>
<xs:element minOccurs="0" name="arg1" type="tns:customer"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="selectLongNameCustomerResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:customer"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="selectMaxIdCustomer">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:customer"/>
<xs:element minOccurs="0" name="arg1" type="tns:customer"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="selectMaxIdCustomerResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:customer"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="selectLongNameCustomer">
<wsdl:part element="tns:selectLongNameCustomer" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="selectLongNameCustomerResponse">
<wsdl:part element="tns:selectLongNameCustomerResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="selectMaxIdCustomer">
<wsdl:part element="tns:selectMaxIdCustomer" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="selectMaxIdCustomerResponse">
<wsdl:part element="tns:selectMaxIdCustomerResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="IHelloService">
<wsdl:operation name="selectLongNameCustomer">
<wsdl:input message="tns:selectLongNameCustomer" name="selectLongNameCustomer"></wsdl:input>
<wsdl:output message="tns:selectLongNameCustomerResponse" name="selectLongNameCustomerResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="selectMaxIdCustomer">
<wsdl:input message="tns:selectMaxIdCustomer" name="selectMaxIdCustomer"></wsdl:input>
<wsdl:output message="tns:selectMaxIdCustomerResponse" name="selectMaxIdCustomerResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloServiceImplServiceSoapBinding" type="tns:IHelloService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="selectLongNameCustomer">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="selectLongNameCustomer">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="selectLongNameCustomerResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="selectMaxIdCustomer">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="selectMaxIdCustomer">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="selectMaxIdCustomerResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloServiceImplService">
<wsdl:port binding="tns:HelloServiceImplServiceSoapBinding" name="HelloServiceImplPort">
<soap:address location="http://localhost:8080/helloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
四、WSDL的构成
一个 WSDL 文档的主要结构是类似这样的:
<definitions> <types>
data type definitions........
</types> <message>
definition of the data being communicated....
</message> <portType>
set of operations......
</portType> <binding>
protocol and data format specification....
</binding> </definitions>
- <wsdl:definitions>是WSDL的根元素,需要关注的元素有name="HelloServiceImplService",属性值为公开的Web服务的实现类+Service,targetNamespace="http://cxf/"指定目标名称空间,还有xmlns:tns="http://cxf/"的属性值就是targetNamespace的属性值,默认使用实现类的包名的反缀,所以其实我该贴一张目录就显而易见了

- <wsdl:types>通过<xs:element>定义Web Service使用的数据类型,<xs:element name="Customer" type="tns:customer"/>其中name是这个复杂类型的JAXB注解的name属性值,type是tns:+JAXB注解的name属性值全小写形式,再往下会看到XXX元素和XXXResponse元素,其中XXX是方法名称,是对方法参数的封装,XXXResponse是对返回值的封装。</xs:complexType>将对XXX和XXXResponse指定封装的内容
- </wsdl:message>将输入参数和响应结果、受检查的异常信息包装为消息
- </wsdl:portType>描述了 web service、可被执行的操作,以及相关的消息。</wsdl:input></wsdl:output>指定输入和输出
- </wsdl:binding>每个端口定义消息格式和协议细节
- 最后的</wsdl:service>name属性指定服务的名称,</wsdl:port>的name指定port名称,<soap:address location="http://localhost:8080/helloWorld"/>指定Web服务的地址
五、客户端
具体代码:
public final class Client {
public static void main(String[] args) throws Exception{
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean ();
factory.setAddress("http://localhost:8080/helloWorld");
factory.setServiceClass(IHelloService.class);
IHelloService o = (IHelloService) factory.create();
Customer c1 = new Customer();
c1.setId(1);
c1.setName("A");
Customer c2= new Customer();
c1.setId(2);
c1.setName("AC");
System.out.println(o.selectMaxIdCustomer(c1,c2).getName());
}
}
使用JaxWsProxyFactoryBean客户端代理工厂调用Web服务,输出:

使用CXF实现WebService的更多相关文章
- 使用CXF发布WebService
这里普及一下WebService和cxf的知识.关于webservice和cxf: WebService.各种提供服务的组件 .企业总线.通讯总线(ESB)CXF:是一个SOA框架,Axi ...
- CXF发布webService服务以及客户端调用
这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...
- 3.使用CXF开发webService
CXF 简介 关于 Apache CXF Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache ...
- Apache CXF实现WebService入门教程(附完整源码)
Apache CXF实现WebService非常简单实用,只需要几步就可以实现一个简单的web service. 首先我们需要新建一个maven项目,在pom中添加依赖和jetty作为测试的web s ...
- 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的两种方式
一.简介 对于基于soap传输协议的webservice有两种开发模式,代码优先和契约优先的模式.代码优先的模式是通过编写服务器端的代码,使用代码生成wsdl:契约优先模式首先编写wsdl,再通过ws ...
- CXF之webservice
使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...
- [置顶] 利用CXF发布webService的小demo
其实webService的发布不仅仅只有xfire,今天,给大家介绍一下用CXF发布一个webService的小demo,CXF也是我做webService用的第一个框架... 先将相关的jar引进来 ...
- 【转】构建基于CXF的WebService服务
构建基于CXF的WebService服务 Apache CXF = Celtix+ XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.C ...
随机推荐
- 2.秋招复习简单整理之String、StringBuffer、StringBuilder的区别和联系
String特点: 1.String是不可变对象,一旦赋值创建就不变,这意味着对String的一切修改将产生一个新的字符串,比如String的subString,replace.toUpperCase ...
- 接口文档注解:@ApiOperation
@ApiOperation不是spring自带的注解是swagger里的 com.wordnik.swagger.annotations.ApiOperation; @ApiOperation和@Ap ...
- vue-cli安装搭建初始项目
vue-cli脚手架 前提:node + npm 安装好 一.介绍 vue-cli: Vue + ESLint + webpack + iview + ES6 Vue:主要框架ESLint:帮助我们检 ...
- 洛谷P1129 [ZJOI2007]矩阵游戏 题解
题目链接:https://www.luogu.org/problemnew/show/P1129 分析: 这道题不是很好想,但只要想的出来,代码不成问题. 思路1 举几个例子,我们发现, 对于任何数来 ...
- UVA11988 【Broken Keyboard (a.k.a. Beiju Text)】:题解
题目链接:https://www.luogu.org/problemnew/show/UVA11988 这题虽说是和链表有关的模拟,但其实并不是很需要啊,但蒟蒻用了(说的好听是练手,说的难听是太弱), ...
- Excel催化剂开源第44波-窗体在Show模式下受Excel操作影响变为最小化解决方式
在Excel催化剂的许多功能中,都会开发窗体用于给用户更友好的交互使用,但有一个问题,困扰许久,在窗体上运行某些代码后,中途弹出下MessageBox对话框给用户做一些简单的提示或交互时,发现程序运行 ...
- [vue折线图] 记录SpringBoot+Vue3.0折线图订单信息展示
因公司业务需求,需要做一份订单相关的折线图, 如果其中有一天没有订单的话,这一天就是空缺的,在绘制折线图的时候是不允许的,所有要求把没有订单数据的日期也要在图表显示. 使用技术vue3.0+sprin ...
- 2019牛客多校第一场 E-ABBA(dp)
ABBA 题目传送门 解题思路 用dp[i][j]来表示前i+j个字符中,有i个A和j个B的合法情况个数.我们可以让前n个A作为AB的A,因为如果我们用后面的A作为AB的A,我们一定也可以让前面的A对 ...
- python函数基础-参数-返回值-注释-01
什么是函数 函数就是有特定功能的工具 # python中有内置函数(python解释器预先封装好的)与自定义函数(用户自定义封装的)之分 为什么要用函数 # 可以减少代码冗余,增加代码复用性 # 使代 ...
- jquery实现最简单的下拉菜单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...