Changing the default XML namespace prefix generated with JAXWS

Pablo Santa Cruz picture Pablo Santa Cruz · Oct 2, 2010 · Viewed 39.2k times · Source

I am using JAXWS to generate a WebService client for a Java Application we're building.

When JAXWS build its XMLs to use in SOAP protocol, it generates the following namespace prefix:

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

My problem is that my Counterpart (a big money transfer company) which manages the server my client is connecting to, refuses to accept the WebService call (please don't ask my why) unless the 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 a way I command JAXWS (or any other Java WS client technology) to generate clients using soapenv instead of env as the XMLNS prefix? Is there an API call to set this information?

Thanks!

Answer

Denian picture Denian · Jan 24, 2011

Maybe it's late for you and I'm not sure if this may work, but you can try.

First you need to implement a SoapHandler and, in the handleMessage method you can modify the SOAPMessage. I'm not sure if you can modify directly that prefix 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;
  }
}

And finally you'll have to add your HandlerResolver to your client service:

Service service = Service.create(wsdlLoc, serviceName);
service.setHandlerResolver(new MyHandlerResolver());