如何在JAX-WS客户端中指定ReplyTo EndpointReference?

时间:2012-05-30 09:09:31

标签: web-services jax-ws ws-addressing

我想使用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来完成它。有什么想法吗?

2 个答案:

答案 0 :(得分: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 >

答案 1 :(得分: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”依赖项。