一、创建Maven工程

注意pom.xml中的dependency配置,需要引入这三个包:(特别注意,引入的这三个包的版本要一致,否则会报奇怪的错误)

<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>com.rvho</groupId><artifactId>cxfstandalone</artifactId><version>0.0.1-SNAPSHOT</version><properties><!-- 文件拷贝编码 --><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- 输出编码 --><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!-- 编译编码 --><maven.compiler.encoding>UTF-8</maven.compiler.encoding><!-- CXF版本 --><cxf.version>3.0.0</cxf.version></properties><dependencies><!-- CXF --><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><!-- 如果CXF不集成到Web服务器中,必须添加该引用 --><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>${cxf.version}</version></dependency><!-- End CXF --></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.3</version><configuration><!-- 指定source和target的jdk版本是1.8 --><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
</project>

二、服务端代码

1、写一个对外发布的接口

package com.cah.ddi3.ws;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;@WebService
public interface WebServiceI {@WebMethodString sayHello(@WebParam(name="name") String name);@WebMethodString saveInfo(@WebParam(name="name") String name,@WebParam(name="info") String info);}

2、实现这个接口

package com.cah.ddi3.ws;import javax.jws.WebParam;
import javax.jws.WebService;@WebService
public class WebServiceImpl implements WebServiceI {@Overridepublic String sayHello(@WebParam(name="name") String name) {// TODO Auto-generated method stubSystem.out.println("WebService sayHello "+name);return "sayHello "+name;}@Overridepublic String saveInfo(@WebParam(name="name") String name,@WebParam(name="info") String info) {// TODO Auto-generated method stubSystem.out.println(name+"调用WebService接口,save:"+info);return "save Success";}
}

3、发布这个服务

package com.cah.ddi3.ws;import javax.xml.ws.Endpoint;import org.apache.cxf.frontend.ServerFactoryBean;public class WebServicePublish {public static void main(String[] args) {String address = "http://localhost:8080/WS_Server/Webservice";Endpoint.publish(address , new WebServiceImpl());System.out.println("发布webservice成功!");
//      ServerFactoryBean sf=new ServerFactoryBean();
//      //服务实现类
//      sf.setServiceClass(WebServiceImpl.class);
//      //服务的发布地址
//      sf.setAddress("http://localhost:8080/WS_Server/Webservice");
//      //服务的实例
//      sf.setServiceBean(new WebServiceImpl());
//      //发布服务
//      sf.create();
//      System.out.println("server ready……");}
}

4、验证

运行上面的类,在浏览器中键入:http://localhost:8080/WS_Server/Webservice?wsdl,如果打开了如下的wsdl文档,说明发布成功。

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ws.ddi3.cah.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="WebServiceImplService" targetNamespace="http://ws.ddi3.cah.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.ddi3.cah.com/" elementFormDefault="unqualified" targetNamespace="http://ws.ddi3.cah.com/" version="1.0">
<xs:element name="saveInfo" type="tns:saveInfo"/>
<xs:element name="saveInfoResponse" type="tns:saveInfoResponse"/>
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="saveInfo">
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
<xs:element minOccurs="0" name="info" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="saveInfoResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="saveInfo">
<wsdl:part element="tns:saveInfo" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="saveInfoResponse">
<wsdl:part element="tns:saveInfoResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="WebServiceI">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello"></wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="saveInfo">
<wsdl:input message="tns:saveInfo" name="saveInfo"></wsdl:input>
<wsdl:output message="tns:saveInfoResponse" name="saveInfoResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WebServiceImplServiceSoapBinding" type="tns:WebServiceI">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="saveInfo">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="saveInfo">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="saveInfoResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WebServiceImplService">
<wsdl:port binding="tns:WebServiceImplServiceSoapBinding" name="WebServiceImplPort">
<soap:address location="http://localhost:8080/WS_Server/Webservice"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

三、客户端代码

创建一个WebService客户端测试项目,借助jdk的wsimort.exe工具生成客户端代码,wsimort.exe工具位于Jdk的bin目录下,打开命令行窗口,切换到src目录,执行"wsimport -keep http://localhost:8080/WS_Server/Webservice?wsdl"生成客户端代码,如下图所示:

在客户端工程的src目录下可以生成如下代码:

创建WsClient.java类,代码如下:

package com.cah.ddi3.ws;public class WsClient {public static void main(String[] args) {// TODO Auto-generated method stubWebServiceImplService factory = new WebServiceImplService();WebServiceI wsImpl = factory.getWebServiceImplPort();String resResult = wsImpl.sayHello("小鱼儿");System.out.println("调用WebService的sayHello方法返回的结果是:"+resResult);System.out.println("---------------------------------------------------");resResult = wsImpl.saveInfo("小鱼儿", "欢迎你!");System.out.println("调用WebService的saveInfo方法返回的结果是:"+resResult);}}

运行上述类,控制台打出:

调用成功!

WebService(CXF对外发布WebService服务)(1)相关推荐

  1. CXF框架发布WebService服务的例子

    1.CXF框架概念介绍 Apache CXF 是一个开源的 WebService 框架,CXF可以用来构建和开发 WebService,这些服务可以支持多种协议,比如:SOAP.POST/HTTP.H ...

  2. Spring和CXF整合发布WebService(服务端、客户端)

    参考Spring和CXF整合发布WebService(服务端.客户端) 转载于:https://www.cnblogs.com/timspace/p/11113576.html

  3. 采用CXF框架发布WebService

    1. CXF介绍 :soa的框架     * cxf 是 Celtrix (ESB框架)和 XFire(webserivice) 合并而成,并且捐给了apache       * CxF的核心是org ...

  4. Maven项目集成cxf框架发布WebService

    关于Maven项目集成cxf框架发布和接收WebService 从网上找了很多,发现大多数都是类似"单机"版的发布,直到看了一篇博客,给我很大的启发. 在此感谢这位博客的作者:ht ...

  5. Azure正式对外发布容器服务,支持Swarm和Mesos

    在2015年9月29日举办的AzureCon大会上,微软曾对外宣布将会发布Azure Container服务(ACS),基于此服务,微软将会把Mesos.Docker和Azure整合在一起.紧接着在2 ...

  6. 使用CXF框架发布SOAP协议的 WebService服务

    引言 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix 和 X ...

  7. cxf动态构建webservice

    title: cxf动态构建webservice date: 2020-06-11 09:58:55 tags: [cxf,webservice,xml/http] categories: [webs ...

  8. WebService -- CXF 总结

    之前使用Dubbo搞过WebService,不过鉴于Dubbo在其他公司使用不频繁,于是开始学习目前比较流程的WebService框架,备选是Axis2,CXF..,最后还是选择了CXF,据说CXF在 ...

  9. webservice+cxf+spring+mysql+注解_WebService:WebService+Springboot常用注解

    这位博主主要讲了WebService的CXF的jar包运用,很实用 @WebService 1.serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:serv ...

最新文章

  1. hp打印机无法与计算机,电脑中安装HP打印机后重启无法打印的解决方法
  2. nagios插件--磁盘IO监控
  3. 如何听节拍器_我是如何开垮一家琴行的!
  4. springmvc 注解总结
  5. 关于测试用例的一些思考
  6. 如何看待蒂姆·库克在苹果的地位
  7. 【MSP是什么】最佳管理实践指南
  8. LINUX编译autoconf
  9. kali安卓手机木马远控
  10. 【深入理解C++】析构函数
  11. 计算机考研江苏,2020江苏高考分数线公布
  12. 英文电子专业词汇(新手必备)
  13. python画聚类树状图_聚类分析python画树状图--Plotly(dendrogram)用法解析
  14. 从MSDN我告诉你下载镜像
  15. 朝圣—保罗· 柯艾略~随记2017-12-6周三
  16. 多益网络社招iq_18年多益秋招iq测试题
  17. pytho sockt编程
  18. java中的几个术语(覆写override,隐藏hiding,重载overload,遮蔽shadowing,遮盖obscuring)
  19. {“errcode“:48001,“errmsg“:“api unauthorized, hints: [ req_id: xxxxxxx]“}
  20. 熊孩子乱敲键盘攻破Linux,“熊孩子”乱敲键盘就攻破了Linux桌面,大神:17年前我就警告过你们...

热门文章

  1. RGBD相机实用问题
  2. 深圳Java培训:Java全链路面试题-第一阶段
  3. 个人档案以及博客声明
  4. asp.net操作office时报错“检索 COM 类工厂中 CLSID 为 {...} 的组件时失败,原因是出现以下错误: 80070005。”
  5. DOTA2 6.86更新日志 史诗级巨变英雄大改
  6. Typora付费,恢复到老版本
  7. ​基于Verilog的DDS波形发生器的分析与实现(三角波、正弦波)
  8. 初识c语言,知识梳理
  9. 有什么适合打游戏的蓝牙耳机?玩游戏不延迟的蓝牙耳机推荐
  10. android应用发短信