首页 > 其他分享 >CXF学习笔记(1)-HelloWorld!-发布webservice

CXF学习笔记(1)-HelloWorld!-发布webservice

时间:2023-11-09 12:06:48浏览次数:49  
标签:CXF HelloWorld server String helloWorldService cxf public webservice


1.apache网站下载CXF


  http://cxf.apache.org/download.html   最新版本2.4.1


2.创建一个java工程,将以下jar包复制到工程的classpath下


  所有的jar包都可以在${CXF_HOME}\lib目录中找到

3.定义服务接口HelloWorldService

因为这个接口将会被我们暴露为webservice,所以给该接口加一个@WebService标注

1. package com.crazycoder2010.webservice.cxf.server;  
2.   
3. import javax.jws.WebParam;  
4. import javax.jws.WebService;  
5.   
6.   
7. /**
8.  * 服务器端对外提供的服务
9.  * @author Kevin_Wang03
10.  *
11.  */  
12. @WebService  
13. public interface HelloWorldService {  
14. /**
15.      * 简单的字符串参数
16.      * @param userName
17.      * @return
18.      */  
19. public String sayHello(@WebParam(name="userName") String userName);  
20.       
21. /**
22.      * 参数为对象的情况
23.      * @param user
24.      * @return
25.      */  
26. public String sayHelloToUser(User user);  
27. }

4.提供具体的webservice提供者HelloWorldServiceImpl

这个实现类实现了我们上面的服务接口,除了要添加@WebService标注外,还要定义该服务的名称serviceName="helloWorldService" 和endpoint(服务接口),其他和普通类没有任何区别

1. package com.crazycoder2010.webservice.cxf.server;  
2.   
3. import javax.jws.WebService;  
4.   
5. /**
6.  * 默认的webservice实现
7.  * 
8.  * @author Kevin_Wang03
9.  * 
10.  */  
11. @WebService(endpointInterface = "com.crazycoder2010.webservice.cxf.server.HelloWorldService", serviceName = "helloWorldService")  
12. public class HelloWorldServiceImpl implements HelloWorldService {  
13.   
14. @Override  
15. public String sayHello(String userName) {  
16. "HelloWorldServiceImpl.sayHello("+userName+")");  
17. return "Hello,"+userName;  
18.     }  
19.   
20. @Override  
21. public String sayHelloToUser(User user) {  
22. "HelloWorldServiceImpl.sayHelloToUser("+user+")");  
23. return "Hello,("+user.getId()+","+user.getName()+")";  
24.     }  
25. }

4.通过JAX-WS将类发布为服务

这个是个普通类,要发布一个webservice,首先要定义好这个webservice的访问地址和端口,然后需要知道把哪个类发布成服务,就这么简单?yes!


1. package com.crazycoder2010.webservice.cxf.server;  
2.   
3. import javax.xml.ws.Endpoint;  
4.   
5. public class Server {  
6. public static void main(String[] args) {  
7. "Starting Server");  
8. new HelloWorldServiceImpl();  
9. "http://localhost:9000/helloWorldService";  
10.         Endpoint.publish(address, helloWorldServiceImpl);  
11. "Start success");  
12.     }  
13. }

5.运行Server类


1. Starting Server  
2. Aug 9, 2011 5:27:29 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass  
3. INFO: Creating Service {http://server.cxf.webservice.crazycoder2010.com/}helloWorldService from class com.crazycoder2010.webservice.cxf.server.HelloWorldService  
4. Aug 9, 2011 5:27:29 PM org.apache.cxf.endpoint.ServerImpl initDestination  
5. INFO: Setting the server's publish address to be http://localhost:9000/helloWorldService  
6. 2011-08-09 17:27:29.895:INFO::jetty-7.4.2.v20110526  
7. Start success

6.通过URL访问WSDL看看是否显示正常

   http://localhost:9000/helloWorldService?wsdl

分析输出的wsdl文件

 6.1<wsdl:definitions name="helloWorldService" > 

       这个name就是我们在HelloWorldServiceImpl类的标注serviceName="helloWorldService"生成的

 6.2<wsdl:definitions targetNamespace="http://server.cxf.webservice.crazycoder2010.com/">

      注意到我们的代码位于com.crazycoder2010.webservice.cxf.server中,记得sun推荐的包的命名标准吗?就是公司域名翻转加上项目名,那把包名翻过来是什么?-你懂得,CXF就用了这种机制

  6.3<xs:complexType name="sayHello">

       这个即是我们定义在服务接口中的方法,在wsdl文件中将接口中的方法定义成服务方法,里面的子元素定义了该方法的参数

  6.4<xs:complexType name="sayHelloResponse">

      这个是对接口服务方法返回值的描述,默认遵循以下约定对方法的描述为sayHello,则返回值的描述为sayHelloResponse

  6.5<wsdl:message name="sayHelloToUser">

      这个描述把服务和方法绑定起来,与Response是成对出现的

  6.6<soap:address location="http://localhost:9000/helloWorldService"/>

     这里就是我们上面在代码里完成的服务的访问地址

1. <?xml version="1.0" ?><wsdl:definitions name="helloWorldService" targetNamespace="http://server.cxf.webservice.crazycoder2010.com/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.cxf.webservice.crazycoder2010.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
2. <wsdl:types>  
3. <xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://server.cxf.webservice.crazycoder2010.com/" xmlns="http://server.cxf.webservice.crazycoder2010.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
4. <xs:complexType name="user">  
5. <xs:sequence>  
6. <xs:element name="id" type="xs:int"></xs:element>  
7. <xs:element minOccurs="0" name="name" type="xs:string"></xs:element>  
8. </xs:sequence>  
9. </xs:complexType>  
10. <xs:element name="sayHelloToUser" type="sayHelloToUser"></xs:element>  
11.   
12. <xs:complexType name="sayHelloToUser">  
13. <xs:sequence>  
14. <xs:element minOccurs="0" name="arg0" type="user"></xs:element>  
15. </xs:sequence>  
16. </xs:complexType>  
17. <xs:element name="sayHelloToUserResponse" type="sayHelloToUserResponse"></xs:element>  
18. <xs:complexType name="sayHelloToUserResponse">  
19. <xs:sequence>  
20. <xs:element minOccurs="0" name="return" type="xs:string"></xs:element>  
21.   
22. </xs:sequence>  
23. </xs:complexType>  
24. <xs:element name="sayHello" type="sayHello"></xs:element>  
25. <xs:complexType name="sayHello">  
26. <xs:sequence>  
27. <xs:element minOccurs="0" name="userName" type="xs:string"></xs:element>  
28. </xs:sequence>  
29. </xs:complexType>  
30. <xs:element name="sayHelloResponse" type="sayHelloResponse"></xs:element>  
31.   
32. <xs:complexType name="sayHelloResponse">  
33. <xs:sequence>  
34. <xs:element minOccurs="0" name="return" type="xs:string"></xs:element>  
35. </xs:sequence>  
36. </xs:complexType>  
37. </xs:schema>  
38. </wsdl:types>  
39. <wsdl:message name="sayHelloToUser">  
40. <wsdl:part element="tns:sayHelloToUser" name="parameters">  
41.   
42. </wsdl:part>  
43. </wsdl:message>  
44. <wsdl:message name="sayHelloToUserResponse">  
45. <wsdl:part element="tns:sayHelloToUserResponse" name="parameters">  
46. </wsdl:part>  
47. </wsdl:message>  
48. <wsdl:message name="sayHelloResponse">  
49. <wsdl:part element="tns:sayHelloResponse" name="parameters">  
50. </wsdl:part>  
51.   
52. </wsdl:message>  
53. <wsdl:message name="sayHello">  
54. <wsdl:part element="tns:sayHello" name="parameters">  
55. </wsdl:part>  
56. </wsdl:message>  
57. <wsdl:portType name="HelloWorldService">  
58. <wsdl:operation name="sayHelloToUser">  
59. <wsdl:input message="tns:sayHelloToUser" name="sayHelloToUser">  
60. </wsdl:input>  
61.   
62. <wsdl:output message="tns:sayHelloToUserResponse" name="sayHelloToUserResponse">  
63. </wsdl:output>  
64. </wsdl:operation>  
65. <wsdl:operation name="sayHello">  
66. <wsdl:input message="tns:sayHello" name="sayHello">  
67. </wsdl:input>  
68. <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">  
69. </wsdl:output>  
70. </wsdl:operation>  
71.   
72. </wsdl:portType>  
73. <wsdl:binding name="helloWorldServiceSoapBinding" type="tns:HelloWorldService">  
74. <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>  
75. <wsdl:operation name="sayHelloToUser">  
76. <soap:operation soapAction="" style="document"></soap:operation>  
77. <wsdl:input name="sayHelloToUser">  
78. <soap:body use="literal"></soap:body>  
79. </wsdl:input>  
80. <wsdl:output name="sayHelloToUserResponse">  
81.   
82. <soap:body use="literal"></soap:body>  
83. </wsdl:output>  
84. </wsdl:operation>  
85. <wsdl:operation name="sayHello">  
86. <soap:operation soapAction="" style="document"></soap:operation>  
87. <wsdl:input name="sayHello">  
88. <soap:body use="literal"></soap:body>  
89. </wsdl:input>  
90. <wsdl:output name="sayHelloResponse">  
91.   
92. <soap:body use="literal"></soap:body>  
93. </wsdl:output>  
94. </wsdl:operation>  
95. </wsdl:binding>  
96. <wsdl:service name="helloWorldService">  
97. <wsdl:port binding="tns:helloWorldServiceSoapBinding" name="HelloWorldServiceImplPort">  
98. <soap:address location="http://localhost:9000/helloWorldService"></soap:address>  
99. </wsdl:port>  
100. </wsdl:service>  
101.   
102. </wsdl:definitions>

小结:

HelloWorld 是跑起来了,感觉比先前用的JAX-RPC要简单的多了,几分钟就可以搞定一个webservice--也许这就是框架的魅力,但是也有一些疑问,如要运行这么小一个helloWorld,要依赖20几个jar包,其中还有Jetty的jar包若干!竟然还有org.eclipse.jetty.*这种包,不知道设计这个框架的同学是怎么考虑的,为什么要把webservice和jetty或eclipse扯上关系,可能看的不够深,明天继续研究。


标签:CXF,HelloWorld,server,String,helloWorldService,cxf,public,webservice
From: https://blog.51cto.com/u_809530/8274920

相关文章

  • Spring整合使用RMI-HelloWorld例子
    Spring整合RMI的原理客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。通过JRMP访问服务。JRMPJRMP:javaremotemethodprotocol,Java特有的,基于流的协议。 服务端暴露远程服务RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通......
  • [MacOS]Ansible-HelloWorld
    安装pipinstallansible手动创建ansible.cfgsudomkdir/etc/ansible/ansible.cfg查看ansible配置信息ansible--version创建hosts文件cd/etc/ansible/sudotouchhostssudovihosts添加主机IP127.0.0.1ansible是基于ssh协议实现的,所以其配置公私......
  • 鸿蒙极速入门(二)-开发准备和HelloWorld
    一、开发准备本篇博客基于的系统版本:华为官方HarmonyOS版本3.1、OpenHarmony版本4.0Beta开发语言ArkTS语言(推荐)JS语言(支持)Java语言(已放弃支持)从Harmony4.0开始,官方主推ArkTS语言,且不再支持Java语言UI框架-方舟开发框架(ArkUI框架)ArkUI框架介绍个人理解:类似iOS的......
  • Java SOAP 调用 C# 的WebService
    JavaSOAP调用C#的WebService,C#的WebService方法的创建可以参考上一篇文章。IntelliJIDEACommunityEdition2021.2.3的idea64.exe新建项目,导入需要的jar,代码如下:importorg.apache.axis.client.Service;importorg.apache.axis.soap.SOAPConstants;importjavax.xml.n......
  • WebService SOAP1.1 SOAP1.12 HTTP PSOT方式调用
    WebServiceSOAP1.1SOAP1.12HTTPPSOT方式调用VisualStudio2022新建WebService项目     创建之后,启动运行 设置默认文档即可 经过上面的创建WebService已经创建完成,添加HelloWorld3方法,[WebMethod]publicstringHelloWorld3(inta,stringb......
  • BitBake使用攻略--从HelloWorld讲起 (转载自:https://www.cnblogs.com/chegxy/p/1571811
    目录写在前面1.什么是BitBake2.BitBake的安装3.使用BitBake构建一个HelloWorld工程后续 写在前面《BitBake使用攻略》系列文章将从今天开始不定时的更新,主要讲解BitBake的背景,基本语法,功能及其命令等知识,旨在为即将从事Yocto项目和OpenEmbedded项目的同学做一些预......
  • HelloWorld
    publicclassHelloWorld{publicstaticvoidmain(String[]args){System.out.println("Hello,World!");//单行注释//输出一个Hello,World!//有趣的代码注释//多行注释:可注释一段文字/*注释*//*我是多......
  • C#调用WebService
     一、前言在日常工作中,如果涉及到与第三方进行接口对接,有的会使用WebService的方式,这篇文章主要讲解在.NETFramework中如何调用WebService。首先我们创建一个WebService,里面有两个方法:一个无参的方法,一个有参的方法:创建好了WebService以后,把WebService部署到IIS上,并确保可......
  • .net 调用webservice 总结
    最近做一个项目,由于是在别人框架里开发app,导致了很多限制,其中一个就是不能直接引用webservice。我们都知道,调用webserivice最简单的方法就是在"引用" 那里点击右键,然后选择"引用web服务",再输入服务地址。确定后,会生成一个app.config里面就会自动生成了一些配置信息。现......
  • C# webservice接口调用实例
    https://blog.csdn.net/qq_43544461/article/details/130768314SOAP协议SOAP(SimpleObjectAccrssProtocol,简单对象访问协议)是一种简单的基于XML的协议,可以使应用程序在分散或分布式的环境中通过HTTP来交换信息。SOAP是WebService的通信协议,SOAP提供了标准的RPC方法来调用WebS......