3

我想使用 JAX-WS API 创建启用 WS-Addressing 的 Web 服务客户端。我使用 wsimport 从 WSDL 文件创建客户端存根,并且可以使用 AddressingFeature 启用/禁用 WS-Addressing,例如

Hello hello = service.getHelloSoap11(new AddressingFeature(true, true));

但是,我在 Web 中找不到任何自定义 WS-Addressing ReplyTo/FaultTo 端点引用的示例。基本上我想创建一个如下所示的 WS 请求(请参阅wsa:ReplyTo元素):

<soapenv:Envelope ...>
  <soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <wsa:To soapenv:mustUnderstand="1">http://localhost:8080/poc/helloService/
    </wsa:To>
    <wsa:ReplyTo>
      <wsa:Address>http://mycompany.com/poc/reply</wsa:Address>
      <wsa:ReferenceParameters>
        <field1 xmlns="http://mycompany.com/poc/cust">some value1</field1>
        <field2 xmlns="http://mycompany.com/poc/cust">some value2</field2>
      </wsa:ReferenceParameters>
    </wsa:ReplyTo>
    <wsa:Action>http://mycompany.com/poc/sayHello</wsa:Action>
    <wsa:MessageID>urn:uuid:7849b04f-c74e-4836-99e4-8e25d2700fae
    </wsa:MessageID>
  </soapenv:Header>
  <soapenv:Body>
    ...
  </soapenv:Body>
</soapenv:Envelope>

如果使用 Spring Web Service 客户端,我可以添加端点引用。但是,我需要使用 JAX-WS 来完成。有任何想法吗?

4

2 回答 2

1

我回答我自己的问题。

似乎标准 JAX-WS API 没有提供一种方便的方式来自定义 WS-Addressing From/ReplyTo/FaultTo 端点引用。但是,每个 JAX-WS 运行时都可能提供额外的专有 API 来设置标头。

例如,IBM JAX-WS RI 提供了EndpointReferenceManager SPI 来创建端点引用:

    import com.ibm.wsspi.wsaddressing.EndpointReference;
    import com.ibm.wsspi.wsaddressing.EndpointReferenceManager;
    import com.ibm.wsspi.wsaddressing.WSAConstants;

    public void testWSAddressing () {

    // get the port
    Hello hello = service.getHelloSoap11();

    // build a EndpiontReference of <wsa:ReplyTo>
    BindingProvider bp = (BindingProvider) hello;
    EndpointReference epr = EndpointReferenceManager.createEndpointReference(new URI(
       "http://www.w3.org/2005/08/addressing/anonymous"));
    epr.setReferenceParameter(new QName("http://mycompany.com/test", "someRefParam"),
                "12345678");

    ((BindingProvider) hello).getRequestContext()
            .put(WSAConstants.WSADDRESSING_REPLYTO_EPR, epr);
    ...

    HelloResponse response = hello.hello(request);
    }

上面的代码在 IBM Websphere 中运行时,将生成如下所示的 SOAP 消息:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <wsa:To>http://localhost:8080/poc/helloService/</wsa:To>
    <wsa:ReplyTo>
      <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous
      </wsa:Address>
      <wsa:ReferenceParameters>
        <someRefParam xmlns="http://mycompany.com/test">12345678</someRefParam>
      </wsa:ReferenceParameters>
    </wsa:ReplyTo>
    <wsa:MessageID>urn:uuid:BE9E173A35BAB51CB31338454394298
    </wsa:MessageID>
    <wsa:Action>http://mycompany.com/Hello</wsa:Action>
  </soapenv:Header>
  <soapenv:Body>
    ...
  </soapenv:Body>
</soapenv:Envelope >
于 2012-07-25T06:34:24.797 回答
1

我找到了一种使用标准 JAX-WS 执行此操作的方法。获取端口时,同时使用 AddressingFeature 和 OneWayFeature。

AddressingFeature addressingfeature = new AddressingFeature();
OneWayFeature onewayfeature = new OneWayFeature(true, new WSEndpointReference(YOUR_REPLY_TO_ADDRESS, AddressingVersion.W3C));

// get the port
Hello hello = service.getHelloSoap11(addressingfeature, onewayfeature);

这将产生带有“ReplyTo”标签的消息。您可能必须为此获取“com.sun.xml.ws:jaxws-rt”依赖项。

于 2014-07-15T13:02:26.600 回答