Java – change the default XML namespace prefix generated using jaxws

I am using jaxws to generate a web service client for the Java application we build

When jaxws builds its XML for use in the soap protocol, it generates the following namespace prefixes:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Body ...>
       <!-- body goes here -->
   </env:Body>
</env:Envelope>

My problem is that my counter part (a large remittance company) manages the server my client is connecting to and refuses to accept web service calls (please don't ask me why), unless xmlns (XML namepspace prefix is soapenv) Like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body ...>
       <!-- body goes here -->
   </soapenv:Body>
</soapenv:Envelope>

So my question is:

Is there any way I can command jaxws (or any other Java WS Client Technology) to generate a client using soapenv instead of Env as an xmlns prefix? Is there an API call to set this information?

thank you!

Solution

Maybe it's too late for you. I don't know if it can work, but you can try

First, you need to implement a soaphandler, and you can modify soapmessage in the handlemessage method I don't know if you can modify the prefix directly, but you can try:

public class MySoapHandler implements SOAPHandler<SOAPMessageContext>
{

  @Override
  public boolean handleMessage(SOAPMessageContext soapMessageContext)
  {
    try
    {
      SOAPMessage message = soapMessageContext.getMessage();
      // I haven't tested this
      message.getSOAPHeader().setPrefix("soapenv");
      soapMessageContext.setMessage(message);
    }
    catch (SOAPException e)
    {
      // Handle exception
    }

    return true;
  }

  ...
}

Then you need to create a handlerresolver:

public class MyHandlerResolver implements HandlerResolver
{
  @Override
  public List<Handler> getHandlerChain(PortInfo portInfo)
  {
    List<Handler> handlerChain = Lists.newArrayList();
    Handler soapHandler = new MySoapHandler();
    String bindingID = portInfo.getBindingID();

    if (bindingID.equals("http://schemas.xmlsoap.org/wsdl/soap/http"))
    {
      handlerChain.add(soapHandler);
    }
    else if (bindingID.equals("http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/"))
    {
      handlerChain.add(soapHandler);
    }

    return handlerChain;
  }
}

Finally, you must add your handlerresolver to your client service:

Service service = Service.create(wsdlLoc,serviceName);
service.setHandlerResolver(new MyHandlerResolver());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>