一、首先创建一个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服务,输出:

最新文章

  1. Database cannot be started in this edition of SQL Server&quot; error when restoring a Microsoft Dynamics CRM database
  2. WPS for Linux(ubuntu)字体缺失解决办法(转)
  3. 多线程_先产后销_运行结果有BUG
  4. 【iOS-cocos2d-X 游戏开发之九】Cocos2dx利用CCSAXParser解析xml数据&amp;CCMutableDictionary使用与注意!
  5. hadoop mapreduce 优化
  6. jQuery 页面加载事件
  7. 浅谈前端中的mvvm与mvc
  8. DSAPI Wifi热点的扫描与连接
  9. Binary Search Tree Learning Summary
  10. Hbase 与Hive整合
  11. Junit的Assert用法
  12. 完整OSW安装方法
  13. 解放你生产力的10款效率工具,赶紧mark起来
  14. Docker容器学习与分享05
  15. Ionic3 UI组件之 ImageLoader
  16. 网络协议之bt---bt协议详解 DHT篇(下)
  17. MapRecude
  18. 跨域拦截Access-Control-Allow-Origin设置多个origin
  19. Delphi 连接 Paradox
  20. Note: OBLIVIATE: A Data Oblivious File System for Intel SGX

热门文章

  1. 为什么我使用 Linux 开发
  2. jieba GitHUb 结巴分词
  3. MySql中的SHOW INDEX 查出的结果列代表的意义
  4. 用Supervisor实现进程守护,在异常退出时自动重启
  5. Java编程思想:XML
  6. Linux下程序下载
  7. NOIP2018普及T2暨洛谷P5016 龙虎斗
  8. TinycoreLinux的安装使用
  9. sql注入篇2
  10. Surging实践经验