使用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 ...
随机推荐
- Oracle Goldengate是如何保证数据有序和确保数据不丢失的?
工作中一直在用Oracle 的中间件Oracle GondenGate 是如何保证消息的有序和不丢失呢? Oracle GoldenGate逻辑架构 首先,先看一下Oracle GoldenGate ...
- 研究Electron主进程、渲染进程、webview之间的通讯
背景 由于某个Electron应用,需要主进程.渲染进程.webview之间能够互相通讯. 不过因为Electron仅提供了主进程与渲染进程的通讯,没有渲染进程之间或渲染进程与webview之间通讯的 ...
- Spring Cloud Alibaba | Nacos服务中心初探
目录 Spring Cloud Alibaba | Nacos服务中心初探 1. 什么是Nacos? 1.1 Nacos 1.0 1.2 Nacos 2.0 2. Nacos 架构及概念 2.1 服务 ...
- 1. Django每日一码 之原生View源码
2019-7-4 今日源码:原生 View 1.URL中调用as_view方法 def as_view(cls, **initkwargs): """ Main ent ...
- shell_umask用法
我曾经用touch命令创建一个文件script,默认的权限是"rw-r- -r- -",有的朋友就有可能问为什么是这种组合?其实,这正是umask命令捣的鬼.在linux上输入:u ...
- restapi(1)- 文件上传下载服务
上次对restapi开了个头,设计了一个包括了身份验证和使用权限的restful服务开发框架.这是一个通用框架,开发人员只要直接往里面加新功能就行了.虽然这次的restapi是围绕着数据库表的CRUD ...
- [算法]Python判断一个点是否在多边形内部
题目: 代码: # -*- coding:utf-8 -*- def rayCasting(p, poly): px = p['x'] py = p['y'] flag = False i = 0 l ...
- [原创]Greenplum数据库集群实践
GreenPlum实践 ============================================== 目录: 一.安装环境准备 二.GP数据库安装 三.集群添加standby节点 四. ...
- [leetcode]95 Unique Binary Search Trees II (Medium)
原题 字母题添加链接描述 一开始完全没有思路.. 百度看了别人的思路,对于这种递归构造的题目还是不熟,得多做做了. 这个题目难在构造出来.一般构造树都需要递归. 从1–n中任意选择一个数当做根节点,所 ...
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...