用于 JAX-WS 应用程序的 wsimport 命令
wsimport 命令行工具用于处理现有 Web Service 描述语言 (WSDL) 文件,并生成开发 Java™ API for XML-Based Web Services (JAX-WS) Web Service 应用程序所必需的工件。所生成的工件符合 Java 5,从而可以在不同的 Java 版本和平台之间移植这些工件。
wsimport 命令行工具支持使用自顶向下的方法开发 JAX-WS Web Service。使用现有 WSDL 文件,通过 wsimport 命令行工具来生成必需的 JAX-WS 工件。


注: 可以对应用程序服务器的 Liberty 和 WebSphere Application Server(传统)使用 wsimport、wsgen、schemagen 和 xjc 命令行工具。
wsimport 工具将读取现有 WSDL 文件并生成以下工件:
- 服务端点接口 (SEI) - SEI 是 Web Service 的 WSDL 文件的已注释 Java 表示法。此接口用于实现 JavaBeans 端点或创建动态代理客户机实例。
- javax.xml.ws.Service 扩展类 - 这是一个已生成的用于扩展 javax.xml.ws.Service 类的类。此类用来配置和创建动态代理和分派实例。
- 必需的数据 Bean,包括对 Web Service 数据进行建模所必需的任何 Java XML 绑定体系结构 (JAXB) Bean。

- 必须在主 WSDL 文件中定义所有服务。wsimport 工具将不处理在导入的 WSDL 文件中定义的服务。
- 如果对实现 Document 或 Literal 样式模式的 WSDL 文件运行 wsimport 工具,那么对输入和输出类型进行定义的 complexTypes 元素必须包含唯一名称,才能防止操作的参数列表中发生命名冲突。
- 如果您运行 wsimport 工具并将 ?wsdl 统一资源标识 (URI) 作为 WSDL 文件的参数来传递,请确保您使用的是已解析的实际 WSDL URI。在完整的概要文件中,wsimport 工具可正确地解析 ?wsdl URI,但所引用的其他相对 URI 可能无法正确解析。 在 Liberty 概要文件中,wsimport 工具可正确地解析 ?wsdl URI 以及所引用的其他相对 URI。
除了从命令行使用这些工具外,还可以从 WebSphere Application Server(传统)的 Ant 构建环境中调用这些 JAX-WS 工具。从 Ant 构建环境中使用 com.sun.tools.ws.ant.WsImport Ant 任务来调用 WsImport 工具。为正常运行,此 Ant 任务需要您使用 ws_ant 脚本调用 Ant。
语法
命令行语法为:
![[Windows]](../images/windows.gif)
app_server_root\bin\wsimport.bat [options] WSDL_URI
![[AIX]](../images/aixlogo.gif)
![[HP-UX]](../images/hpux.gif)
![[Linux]](../images/linux.gif)
![[Solaris]](../images/solaris.gif)
app_server_root/bin/wsimport.sh [options] WSDL_URI
![[IBM i]](../images/iseries.gif)
app_server_root/bin/wsimport [options] WSDL_URI
参数
WSDL_URI 是需要的唯一参数。对于 wsimport 命令,以下参数是可选参数:
- -b <path>
- 指定外部 JAX-WS 或 JAXB 绑定文件。可以通过使用 -b 选项来指定多个 JAX-WS 和 JAXB 绑定文件;但是每个文件都必须通过它自己的 -b 选项来指定。
- -B <jaxbOption>
- 指定将此选项传递到 JAXB 模式编译器。
- -catalog
- 指定目录文件以解析外部实体引用。它支持 TR9401、XCatalog 和 OASIS XML 目录格式。
- -d <directory>
- 指定用于放置生成的输出文件的位置。
- -extension
- 指定是否接受对不是由 JAX-WS 规范指定的功能进行定制扩展。使用定制扩展可能会产生不可移植的应用程序或者不与其他实现互操作的应用程序。
- -help
- 显示帮助菜单。
- -httpproxy:<host>:<port>
- 指定 HTTP 代理。缺省端口值为 8080。
- -keep
- 指定是否保留已生成的源文件。
- -p <package_name>
- 使用此命令行选项指定一个目标包,并覆盖在 JAX-WS 规范中定义的包名和缺省包名算法的所有 WSDL 文件和模式绑定定制。
- -quiet
- 指定禁止 wsimport 输出。
- -s <directory>
- 指定用于放置生成的源文件的目录。
- -target <version>
- 指定此选项以生成符合特定 JAX-WS 规范级别的代码。指定 V2.0 或 V2.1 以分别生成符合 JAX-WS 2.0 或 JAX-WS 2.1 规范的代码。指定 V2.1 时,指示要生成符合 JAX-WS 2.1 规范的代码。缺省值为 V2.2,用于生成符合 JAXB 2.2 规范的代码。
- target 选项对于 Liberty 概要文件为必需。
- -verbose
- 指定此选项以输出一些消息来说明编译器正在执行哪些操作。
- -version
- 打印版本信息。如果指定此选项,那么输出中将只包含版本信息,并且不会进行正常的命令处理。
- -wsdlLocation
- 指定 @WebServiceClient.wsdlLocation 值。
支持的配置: 在缺省情况下或指定了 -wsdlLocation 属性的情况下,wsimport 工具不会设置 @WebsService.wsdlLocation 值。wsimport 命令行工具仅更新 @WebServiceClient.wsdlLocation 注释。使用相对 URL 来手动更新 @WebService.wsdlLocation 注释,该相对 URL 指定了 Web Service 描述语言 (WSDL) 文件的位置。如果 @WebService.wsdlLocation 注释存在于端点实现类上,那么值必须是相对 URL 并且该相对 URL 所引用的 WSDL 文档必须与应用程序封装在一起。sptcfg
避免故障: 如果对 -wsdlLocation 参数指定 HTTPS URL,那么 wsimport 工具将生成包含无效的无参数构造函数的服务类。避免使用无参数的服务构造函数对服务进行实例化。改为将 HTTPS URL 传递到使用 WSDL URL 作为参数的某个服务类构造函数,例如:
gotchaMyService("https://example.ibm.com/My?wsdl");
- -clientJar
- wsimport on service WSDL generates the necessary classes that are needed for web service invocation. But not all the information that is required by the JAX-WS runtime is captured in the SEI or the WebServiceClient through standard web service annotations. The generated class has the location of the WSDL, so that it can retrieve the additional metadata of the service at runtime. However, an extra connection is required just to access the metadata each time a service instance is created, which increases the network usage. By using the -clientJar option for wsimport, the WSDL and schema are automatically downloaded and all the generated client-side artifacts are packaged into a JAR file. Therefore, you can include the generated JAR file by using this new option in the class path and do not need to fetch the WSDLs from the end point each time a service instance is created, thus saving on network usage.