为 JAX-WS 客户机开发部署描述符

部署描述符是标准文本文件,使用 XML 进行格式化,并打包在 Web Service 应用程序中。可选择使用 Web Services for Java™ Platform, Enterprise Edition (Java EE) 规范 (JSR 109) 服务引用部署描述符来扩充或覆盖在 Java API for XML-Based Web Services (JAX-WS) Web Service 客户机中的注释中指定的应用程序元数据。

开始之前

必须首先使用 wsimport 命令根据 Web Service 描述语言 (WSDL) 文件生成 Web Service 客户机工件。

关于此任务

可以在 application-client.xmlweb.xmlejb-jar.xml Java EE 部署描述符中添加 service-ref 条目。service-ref 条目表示对 webEnterprise JavaBeans (EJB) 或应用程序客户机容器中的 Java EE 组件使用的 Web Service 的引用。service-ref 条目具有用于查找服务的 JNDI 名称。指定 service-ref 条目使客户机应用程序可以使用 JNDI 查询找到服务,并且还可以使用这些服务引用来进行资源注入。

对于在某个部署描述符中找到的每个 service-ref 条目,相应的服务对象将绑定到 JNDI 名称空间,并且在指定端口的情况下包含端口信息。JAX-WS 客户机现在可以执行 JNDI 查询以检索 JAX-WS 服务或端口实例。

当定义用于表示 JAX-WS 服务的 service-ref 时,请将 wsimport 工具所生成的 javax.xml.ws.Service 子类用作 service-interface 值。 这是包含 @WebServiceClient 注释的类。当定义用于表示 JAX-WS 端口的 service-ref 时,service-interface 值仍是 wsimport 工具生成的 javax.xml.ws.Service 子类,而 service-ref-type 值指定端口使用的服务端点接口 (SEI) 类。SEI 类也由 wsimport 生成,并且它由 @WebService 注释进行注释。

过程

  1. application-client.xmlweb.xmlejb-jar.xml 部署描述符中定义 service-ref 条目。
    例如,假设 Web 应用程序归档 (WAR) 文件包含具有以下 service-ref 条目的 WEB-INF/web.xml 部署描述符:
    <service-ref>
      <service-ref-name>service/ExampleService</service-ref-name>
      <service-interface>com.ibm.sample.ExampleService</service-interface>
    </service-ref>
    
    <service-ref>
      <service-ref-name>service/ExamplePort</service-ref-name>
      <service-interface>com.ibm.sample.ExampleService</service-interface>
      <service-ref-type>com.ibm.sample.ExamplePort</service-ref-type>
    </service-ref>
    
    <service-ref>
      <service-ref-name>service/ExamplePortInjected</service-ref-name>
      <service-interface>com.ibm.sample.ExampleService</service-interface>
      <service-ref-type>com.ibm.sample.ExamplePort</service-ref-type>
    
      <injection-target>
        <injection-target-class>com.ibm.sample.J2EEClient</injection-target-class>
        <injection-target-name>injectedPort</injection-target-name>
      </injection-target>
    </service-ref>
    在此示例中,com.ibm.sample.ExampleService 是生成的 JAX-WS 服务类并且此类必须是 javax.xml.ws.Service 的子类。 另外,ExampleService.getPort() 方法返回 com.ibm.sample.ExamplePort 的实例。
  2. 在 Web Service 客户机应用程序中使用部署描述符以定制应用程序。 以下代码片段是客户机应用程序可以如何使用 WAR 模块中的 service-ref 条目的示例:
    import javax.xml.ws.Service;
    import com.ibm.sample.ExampleService;
    import com.ibm.sample.ExamplePort;
    
    // Create an InitialContext object for doing JNDI lookups.
    InitialContext ic = new InitialContext();
    
    // Client obtains an instance of the generic service class using JNDI.
    Service genericService =
    (Service) ic.lookup(“java:comp/env/service/ExampleService”);
    
    // Client obtains an instance of the generated service class using JNDI.
    ExampleService exampleService =
    (ExampleService) ic.lookup(“java:comp/env/service/ExampleService”);
    
    // Client obtains an instance of the port using JNDI.
    ExamplePort ExamplePort =
    (ExamplePort) ic.lookup(“java:comp/env/service/ExamplePort”);
    
    // The container injects an instance of ExamplePort based on the client deployment descriptor
    private ExamplePort injectedPort;

结果

现在可以使用在客户机应用程序中的部署描述符中定义的服务引用。另外,可以使用部署描述符来扩充或覆盖 @WebServiceRef@Resource 注释指定的信息。

<lookup-name> 部署描述符元素是 Java EE 6 中的新增内容,用于间接引用已定义的服务引用。 当使用 <lookup-name> 元素时,只能还指定 <service-ref-name> 元素,不能定义 <service-ref> 的任何其他子元素。

以下示例显示了 WEB-INF/web.xml 文件内的 service-ref 条目(用于定义对 JAX-WS 服务的引用)以及同一 web.xml 文件内的 service-ref 条目(用于定义对第一个 service-ref 的间接引用):

<service-ref>
  <service-ref-name>service/ExampleService</service-ref-name>
  <service-interface>com.ibm.sample.ExampleService</service-interface>
  <service-ref-type>com.ibm.sample.ExampleServicePortType</service-ref-type>
  <wsdl-file>WEB-INF/wsdl/ExampleService.wsdl</wsdl-file>
</service-ref>

<service-ref>
  <service-ref-name>service/ExampleService2</service-ref>
  <lookup-name>java:comp/env/service/ExampleService</lookup-name>
</service-ref>

假设上面的 service-refWEB-INF/web.xml 文件中定义,客户机应用程序可以使用名称 java:comp/env/service/ExampleService2 来执行 JNDI 查找,并且结果将是对 WSDL 文档 WEB-INF/wsdl/ExampleService.wsdlExampleService 服务的引用(如第一个 service-ref 中定义)。

下一步做什么

通过编写用于调用 Web Service 的客户机应用程序代码来完成客户机实现。


指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_jaxwsclientdd
文件名:twbs_jaxwsclientdd.html