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

标签 java web-services jax-ws axis2 wsimport

首先,我想让您知道我已经可以连接到 Web 服务服务器。我问这个问题是因为我想更深入地了解 wsimport 生成的客户端是如何工作的。根据我的研究,wsimport 使用 JAXWS。请注意,我不了解 JAXWS。

我使用 wsimport 生成了我的客户端。我使用的 WSDL 来自 Axis2 Web 服务,由 Axis2 自动生成。下面的类是wsimport的结果:

com.datamodel.xsd

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

com.service

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

通过上面的类,我可以看出 com.datamodel.xsd 包含 Web 服务服务器使用的 bean(不包括 ObjectFactorypackage-信息)。同时,MyMethodMyMethodResponse 也是用于设置 Web 服务方法/操作的请求和响应参数的 bean。

以下是我的问题:(如果您不知道我的某些问题的答案,您实际上不必回答所有问题。:) 请随时分享您认为我可能找到的任何信息有用。)

我说得对吗

  • 我的上述假设是否正确?
  • 其他类的作用是什么?
  • 我检查了 MyWebService,它包含一个注释,它引用了我用来生成客户端的 WSDL 的绝对位置。在客户端指定 wsdllocation 有什么相关性?客户如何使用这些信息?
  • 我注意到 Web 服务的实际 URL 没有在任何生成的类中声明。客户端如何知道它需要连接到哪里?
  • 是否对 WSDL 文件进行了注释,以便客户端可以在连接时读取 WSDL 文件上的 URL?如果是这样,是否意味着必须建立新连接时始终读取 WSDL 文件?
  • 由于我需要编译我的应用程序并将其安装在不同的服务器上,因此将变得无效。我可以将其设置为相对路径而不是绝对路径吗?如何? (答:可以,可以设置为相对路径。wsimport命令有一个wsdllocation属性,其中wsdllocation的值可以是指定。)
  • 如果我需要连接到 HTTPS,该怎么办。如何设置服务器证书?
  • 使用 wsimport 生成客户端和使用 Axis2 或 Apache CXF 生成客户端时有什么不同吗?

最佳答案

在我回答问题之前,先澄清一下:JAX-WS 是用于在 Java 中实现 Web 服务的规范。它描述了如何将 WSDL 工件映射到 Java 类以及如何使用注释来应用这种映射。您可以download the specification here .工具 wsimport 是本规范引用实现的一部分,引用实现是 Java 类库的一部分。有几种替代实现,例如 Axis2、CXF 或 Metro,通过支持其他标准(例如 WS-ReliableMessaging 或 WS-Security)来增强基本 JAX-WS 支持。

现在回答你的问题:

Am I correct with my assumptions above?

是的,你是。

What are the function of the other classes?

package-info 用于将 Web 服务中使用的 XML 命名空间映射到实现类所在的包。命名空间通常看起来与 Java 包名称不同(通常是 URL),这使得映射成为必要。

ObjectFactory 允许您创建服务发送和接收的任何消息。如果你想在你的 stub 类前面挂上代码,提供修改过的消息或类似的东西,你需要这个。

我看不到您的类的内容,但如果我理解正确的话,MyWebServicePortType 是一个类似于 WSDL 中的 portType 的接口(interface)。也就是说,它将 WSDL 中的操作及其签名映射到 Java 方法。如果您想提供服务(您不提供服务,您正在询问客户端),则需要实现此接口(interface)。在实现客户端时,您只需使用它。

最后,MyWebService 类包含您想要调用 Web 服务所需的客户端 stub 。

I inspected MyWebService and it contains an annotation referring to the absolute location of the WSDL I used to generate the client. What is the relevance of specifying the wsdllocation in the client? How does the client use that info?

您生成的接口(interface)包含服务的 portType 的签名,但它没有说明您如何与服务通信。这是 WSDL 中绑定(bind)的一部分。最基本的设置是使用 SOAP over HTTP 的消息的文档/文字样式。其他配置(例如 JMS 上的 SOAP)也是可能的,您的客户端需要知道要使用什么协议(protocol)。因此它需要绑定(bind) WSDL。此外,正如您稍后所述,您的 Java 文件中没有端点地址。这个地址也是从 WSDL 中读取的。

I noticed that the actual URL of the web service is not declared in any of the classes generated. How does the client know where it needs to connect to?

它从 WSDL 中 serviceport 读取 address。它位于 WSDL 的末尾。

Was the WSDL file annotated so that the client can read the URL on the WSDL file upon connection?

不,port 是具体 Web 服务端点的典型元素。这里没有什么特别需要的。

If so, then does it mean that the WSDL file is always read when a new connection must be established?

嗯,客户端可能有缓存(我不知道这个引用实现的细节)。从概念的角度来看:是的。

What if I need to connect to an HTTPS. How can I set the server certificate

这可能很棘手,我不能给你一个开箱即用的答案。我建议通读有关此主题的问题,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 更好,不要使用 wsdl2java。 Here is a description, why .

关于java - wsimport 生成的客户端如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12241781/

相关文章:

java - 如何使用字符串从数组列表中获取数据

java - Unity C# 和 Layar SDK Java - AndroidJavaException

java - 带有 Jersey 的 Tomcat 上的错误

java - 如何在 WSDL 中添加 xsd

java - 如何使用 Netbeans 8.0 导出 Java Web 服务?

java - 从文档中运行 jax-ws 示例时出现异常

java - HTTP 状态 500 - 在 eclipse maven 中实例化 servlet 时出错

java - Axis 2 中 SOAP 消息的加密

java - 具有 Axis 服务的 JAX-WS 客户端

java - HttpClient 检查 Kerberos 安全网页。 NTLM 登录无效