一、首先创建一个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的更多相关文章

  1. 使用CXF发布WebService

    这里普及一下WebService和cxf的知识.关于webservice和cxf:   WebService.各种提供服务的组件     .企业总线.通讯总线(ESB)CXF:是一个SOA框架,Axi ...

  2. CXF发布webService服务以及客户端调用

    这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...

  3. 3.使用CXF开发webService

    CXF 简介 关于 Apache CXF Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache ...

  4. Apache CXF实现WebService入门教程(附完整源码)

    Apache CXF实现WebService非常简单实用,只需要几步就可以实现一个简单的web service. 首先我们需要新建一个maven项目,在pom中添加依赖和jetty作为测试的web s ...

  5. struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

    Cxf + Spring+ myeclipse+ cxf 进行  Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...

  6. 使用cxf开发webservice应用时抛出异常

    在使用cxf开发webservice应用时,报出了类似下面的错误 JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:& ...

  7. cxf构建webservice的两种方式

    一.简介 对于基于soap传输协议的webservice有两种开发模式,代码优先和契约优先的模式.代码优先的模式是通过编写服务器端的代码,使用代码生成wsdl:契约优先模式首先编写wsdl,再通过ws ...

  8. CXF之webservice

    使用 CXF 做 webservice 简单例子     Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...

  9. [置顶] 利用CXF发布webService的小demo

    其实webService的发布不仅仅只有xfire,今天,给大家介绍一下用CXF发布一个webService的小demo,CXF也是我做webService用的第一个框架... 先将相关的jar引进来 ...

  10. 【转】构建基于CXF的WebService服务

    构建基于CXF的WebService服务 Apache CXF = Celtix+ XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.C ...

随机推荐

  1. redux和react-redux做简单组件控制

    这次我们用两种方式实现以下要求 1.三个组件 2.第一个组件有两个按钮 分别控制第二和第三个组件年龄和姓名的改变 3第二个组件展示姓名,第三个组件展示年龄 用到哪些插件 store  redux 一 ...

  2. NEST 6.X升级到7.X

    升级比对可访问 NEST 6.X升级到7.X 查看 ElasticClient-CreateIndex 升级前代码,NEST版本6.6.0 ICreateIndexResponse response ...

  3. django基础知识之HTML转义:

    HTML转义 Django对字符串进行自动HTML转义,如在模板中输出如下值: 视图代码: def index(request): return render(request, 'temtest/in ...

  4. springboot2.x纯注解整合dubbo

    springboot1.x和springboot2.x整合差距挺大的,基于最新的2.x进行整合,使用纯注解的方式 依赖选取 首先pom文件的依赖引入,maven仓库有Apache和alibaba两个 ...

  5. 使用OpenAPI构建更智能的API

    像OpenAPI这样的API描述规范是一个关键工具,您应该尽可能地将其好好掌握,记录和执行API的工作由计算机和开发人员完成:OpenAPI 3.0现在允许额外的表现力,可以让机器为我们做更多有用的工 ...

  6. nodejs中文乱码问题

    node.js暂时不支持GBK或gb2312,所以编程文件(js)需要修改为utf-8格式. 另外如需要返回html代码,在 writeHead 方法中加入 "charset=utf-8&q ...

  7. markdown的博客

    测试一下markdown写博客 function firstProgram() { console.log("This is my first markdown blog"); }

  8. 大数据 SQL Boy 脱坑指南

    不可否认的是 SQL 是一个伟大的发明,它让增删改查的操作更加地便捷化,而且 SQL 的学习成本相对其他编程语言来说较低,被逼到会写 SQL 的运营和产品我都见过不少... 大数据行业跟 SQL 更是 ...

  9. 调用scanf函数的一个陷阱

    我们在写C程序时,经常使用scanf函数,让用户输入数据,可是有时候会出现一些很奇怪的问题.例如,下面的程序是一个简单的四则运算: #include <stdio.h> int main( ...

  10. Linux中的保护机制

    Linux中的保护机制 在编写漏洞利用代码的时候,需要特别注意目标进程是否开启了NX.PIE等机制,例如存在NX的话就不能直接执行栈上的数据,存在PIE 的话各个系统调用的地址就是随机化的. 一:ca ...