How to specify ReplyTo EndpointReference in a JAX-WS client?

假装没事ソ 提交于 2019-12-06 04:05:21

I answer my own question.

It seems that the standard JAX-WS API does not provide a convenient way to customize the WS-Addressing From/ReplyTo/FaultTo endpoint references. However, each JAX-WS runtime may provide additional proprietary API to set the headers.

For example, the IBM JAX-WS RI provides an EndpointReferenceManager SPI to create the endpoint reference:

    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);
    }

The above code, when running inside IBM Websphere, will produce a SOAP message like the following:

<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 >

I've found a way to do this with standard JAX-WS. When getting a port, use both AddressingFeature and 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);

This will produce messages with "ReplyTo" tag. You may have to grab "com.sun.xml.ws:jaxws-rt" dependency for this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!