简体   繁体   English

wsimport生成的客户端如何工作?

[英]How does a wsimport generated client work?

Before anything else, I want you to know that I can already connect to the web service server. 在此之前,我想让您知道我已经可以连接到Web服务服务器了。 I'm asking this question because I want to gain a deeper knowledge on how a wsimport generated client works. 我问这个问题是因为我希望深入了解wsimport生成的客户端是如何工作的。 Based from my research, wsimport uses JAXWS. 根据我的研究,wsimport使用JAXWS。 Please note that I have no knowledge from JAXWS. 请注意,我不了解JAXWS。

I generated my client using wsimport. 我使用wsimport生成了我的客户端。 The WSDL I used is from an Axis2 web service and was automatically generated by Axis2. 我使用的WSDL来自Axis2 Web服务,由Axis2自动生成。 The classes below are the results of wsimport: 下面的类是wsimport的结果:

Under com.datamodel.xsd com.datamodel.xsd

  • DataBeanRequest.java
  • DataBeanResponse.java
  • ObjectFactory.java
  • package-info.java

Under com.service com.service

  • MyWebService.java
  • MyWebServicePortType.java
  • MyMethod.java
  • MyMethodResponse.java
  • ObjectFactory.java
  • package-info.java

With the classes above, I can that tell that com.datamodel.xsd contains the beans used by the web service server (excluding ObjectFactory and package-info ). 使用上面的类,我可以告诉com.datamodel.xsd包含Web服务服务器使用的bean(不包括ObjectFactorypackage-info )。 Meanwhile, MyMethod and MyMethodResponse are also beans used to set the request and response parameter of the web service method/operation. 同时, MyMethodMyMethodResponse也是用于设置Web服务方法/操作的请求和响应参数的bean。

Below are my questions: (You don't really have to answer all of it if you don't know the answers on some of my questions. :) And please feel free to share any info that you think I might find useful.) 以下是我的问题:(如果你不知道我的一些问题的答案,你真的不必回答所有问题。:)请随时分享你认为我可能会觉得有用的任何信息。)

Am I correct with 我是否正确

  • Am I correct with my assumptions above? 我的上述假设是否正确?
  • What are the function of the other classes? 其他课程的功能是什么?
  • I inspected MyWebService and it contains an annotation referring to the absolute location of the WSDL I used to generate the client. 我检查了MyWebService ,它包含一个注释,指的是我用来生成客户端的WSDL的绝对位置。 What is the relevance of specifying the wsdllocation in the client? 在客户端中指定wsdllocation的相关性是什么? How does the client use that info? 客户如何使用该信息?
  • I noticed that the actual URL of the web service is not declared in any of the classes generated. 我注意到Web服务的实际URL未在生成的任何类中声明。 How does the client know where it needs to connect to? 客户如何知道需要连接的位置?
  • Was the WSDL file annotated so that the client can read the URL on the WSDL file upon connection? 是否对WSDL文件进行了注释,以便客户端可以在连接时读取WSDL文件上的URL? If so, then does it mean that the WSDL file is always read when a new connection must be established? 如果是这样,那么是否意味着在必须建立新连接时始终读取WSDL文件?
  • Since there's a need for me to compile my application and install it on a different server, the will become invalid. 由于我需要编译我的应用程序并将其安装在不同的服务器上,因此将无效。 Can I set it to a relative path instead of an absolute path? 我可以将其设置为相对路径而不是绝对路径吗? How? 怎么样? (Answer: Yes, it can be set to a relative path. The wsimport command has a wsdllocation attribute wherein the value of the wsdllocation can be specified.) (答案:是的,它可以被设置为一个相对路径的。 wsimport命令有一个 wsdllocation属性,其中所述的值 wsdllocation可以被指定。)
  • What if I need to connect to an HTTPS. 如果我需要连接到HTTPS,该怎么办? How can I set the server certificate? 如何设置服务器证书?
  • Is there any difference when I generate my client using wsimport and when I generate it using Axis2 or Apache CXF. 当我使用wsimport生成客户端时以及使用Axis2或Apache CXF生成客户端时有什么区别。

Before I answer the questions, some clarification: JAX-WS is a specification for implementing web services in Java. 在回答问题之前,请先澄清一下:JAX-WS是用Java实现Web服务的规范。 It describes how WSDL artifacts can be mapped to Java classes and how this mapping can be applied using annotations. 它描述了如何将WSDL工件映射到Java类以及如何使用注释应用此映射。 You can download the specification here . 您可以在此处下载规范 The tool wsimport is part of the reference implementation of this specification and the reference implementation is part of the Java class library. 工具wsimport是本规范的参考实现的一部分,参考实现是Java类库的一部分。 There are several alternative implementations, such as Axis2, CXF or Metro, that enhance the basic JAX-WS support with support for additional standards such as WS-ReliableMessaging or WS-Security. 有几种替代实现,例如Axis2,CXF或Metro,它们通过支持其他标准(如WS-ReliableMessaging或WS-Security)来增强基本的JAX-WS支持。

Now to your questions: 现在回答你的问题:

Am I correct with my assumptions above? 我的上述假设是否正确?

Yes, you are. 是的,你是。

What are the function of the other classes? 其他课程的功能是什么?

The package-info exists to map the XML namespace used in the web service to the package in which your implementation classes reside. package-info用于将Web服务中使用的XML命名空间映射到实现类所在的包。 The namespace normally looks different from a Java package name (normally, it is a URL) and this makes the mapping necessary. 命名空间通常看起来与Java包名称不同(通常是URL),这使映射成为必要。

The ObjectFactory allows you to create any of the messages sent and received by the service. ObjectFactory允许您创建服务发送和接收的任何消息。 You need this if you want to hook in code in front of your stub class, provide modified messages or similar things. 如果要在存根类前面挂钩代码,提供修改后的消息或类似的东西,则需要这样做。

I can't see the content of your classes, but if I understand it right MyWebServicePortType is an interface that resembles the portType in your WSDL. 我无法看到您的类的内容,但如果我理解它, MyWebServicePortType是一个类似于WSDL中的portType的接口。 That is, it maps the operations and their signatures in the WSDL to Java methods. 也就是说,它将WSDL中的操作及其签名映射到Java方法。 If you want to provide the service (which you don't, you are asking about the client), you would need to implement this interface. 如果您想提供服务(您不需要提供服务,请询问客户端),您需要实现此接口。 As you implement the client, you simply use it. 在实现客户端时,您只需使用它。

Finally, the class MyWebService contains the client stub you need if you want to invoke the web service. 最后,如果要调用Web服务,类MyWebService包含您需要的客户端存根。

I inspected MyWebService and it contains an annotation referring to the absolute location of the WSDL I used to generate the client. 我检查了MyWebService,它包含一个注释,指的是我用来生成客户端的WSDL的绝对位置。 What is the relevance of specifying the wsdllocation in the client? 在客户端中指定wsdllocation的相关性是什么? How does the client use that info? 客户如何使用该信息?

The interface you generated contains the signature of the portType of the service, but it does not explain how you can talk to the service. 您生成的接口包含服务的portType的签名,但它不解释您如何与服务进行通信。 This is part of the binding in the WSDL. 这是WSDL中绑定的一部分。 The most basic setting is a document/literal style for the messages using SOAP over HTTP. 最基本的设置是使用SOAP over HTTP的消息的文档/文字样式。 Other configurations, such as SOAP over JMS, are possible and your client needs to know what protocol to use. 其他配置(例如SOAP over JMS)是可能的,您的客户端需要知道要使用的协议。 Therefore it needs the binding WSDL. 因此它需要绑定WSDL。 Also, as you state later, there is no endpoint address in your Java files. 此外,正如您稍后所述,Java文件中没有端点地址。 This address is also read from the WSDL. 此地址也是从WSDL中读取的。

I noticed that the actual URL of the web service is not declared in any of the classes generated. 我注意到Web服务的实际URL未在生成的任何类中声明。 How does the client know where it needs to connect to? 客户如何知道需要连接的位置?

It reads the address from the port of the service in the WSDL. 它从WSDL中的service port读取address This is located of the end of the WSDL. 这位于WSDL的末尾。

Was the WSDL file annotated so that the client can read the URL on the WSDL file upon connection? 是否对WSDL文件进行了注释,以便客户端可以在连接时读取WSDL文件上的URL?

No, the port is a typical element of a concrete web service endpoint. 不, port是具体Web服务端点的典型元素。 There's nothing special needed here. 这里没有什么特别的需要。

If so, then does it mean that the WSDL file is always read when a new connection must be established? 如果是这样,那么是否意味着在必须建立新连接时始终读取WSDL文件?

Well, there could be caching at the client side (I don't know about the details of the reference implementation on this one). 好吧,客户端可能有缓存(我不知道这个参考实现的细节)。 From a conceptual point of view: yes, it is. 从概念的角度来看:是的,它是。

What if I need to connect to an HTTPS. 如果我需要连接到HTTPS,该怎么办? How can I set the server certificate 如何设置服务器证书

This can be tricky, I can't give you an out-of-the-box answer. 这可能很棘手,我不能给你一个开箱即用的答案。 I would suggest to read through questions on this topic, such as this one . 我建议阅读有关此主题的问题, 例如本主题。

Is there any difference when I generate my client using wsimport and when I generate it using Axis2 or Apache CXF 当我使用wsimport生成客户端时以及使用Axis2或Apache CXF生成客户端时有什么区别

Yes, there is. 就在这里。 wsimport is better, don't use wsdl2java. wsimport更好,不要使用wsdl2java。 Here is a description, why . 这是一个描述,为什么

You asked: I noticed that the actual URL of the web service is not declared in any of the classes generated. 您问:我注意到Web服务的实际URL未在生成的任何类中声明。 How does the client know where it needs to connect to? 客户如何知道需要连接的位置?

If the WSDL was downloaded using a browser and passed as input to wsimport , then the local wsdl file location is embedded into the generated code. 如果使用浏览器下载WSDL并将其作为输入传递给wsimport ,则将本地wsdl文件位置嵌入到生成的代码中。 That is why you don't see the actual service location in the generated code. 这就是您在生成的代码中看不到实际服务位置的原因。 It also means if you deleted the local copy of the wsdl file the generated code will not work (when inovked using a main method) . 这也意味着如果删除了wsdl文件的本地副本,则生成的代码将无法工作(使用main方法进行inovked时)。

If the URL of the wsdl was passed as input to wsimport then that URL is embedded in the generated code, which is further used to get the actual service location. 如果将wsdl的URL作为输入传递给wsimport,则该URL嵌入在生成的代码中,该代码还用于获取实际的服务位置。 The idea is that the WSDL locations are fixed. 我们的想法是WSDL位置是固定的。 They are expected to be in a UDDI or as a local file. 它们应该在UDDI中或作为本地文件。 This allows the actual services to move around and if they do move, you just have to modify the local copy of the wsdl file alone or update the wsdl in the UDDI. 这允许实际服务移动,如果它们移动,您只需要单独修改wsdl文件的本地副本或更新UDDI中的wsdl。 [mostly this does not happen as service locations are never IP but DNS names] [大多数情况下这不会发生,因为服务地点永远不是IP而是DNS名称]

This is why it is never a good idea to publish the wsdl in the same server where your webservice is running 这就是为什么在运行webservice的同一服务器中发布wsdl永远不是一个好主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM