0

is possible in JAX-WS to generate xmlns attributes instead of prefixes?

Example: Object A from package myns.a contains some objects B1, B2 from package myns.b. Generated SOAP message:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="urn:myns/a" xmlns:b="urn:myns/b">
   <soapenv:Header/>
   <soapenv:Body>
      <a:A1>
         <b:B1>123456</b:B1>
         <b:B2>abc</b:B2>  
      </a:A1>
   </soapenv:Body>
</soapenv:Envelope>

However, i need to generate it this way (so prefix b should be removed and all objects from package myns.b should have xmlns attribute):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="urn:myns/a">
   <soapenv:Header/>
   <soapenv:Body>
      <a:A1>
            <B1 xmlns="urn:myns/b">123456</B1>
            <B2 xmlns="urn:myns/b">abc</B2>
      </a:A1>
   </soapenv:Body>
</soapenv:Envelope>

Is there a simple way, how to handle this? For example on package-info.java level?

mrq
  • 13
  • 3
  • These two XML fragments are equivalent (in terms of namespaces). [So what exactly is the problem?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Seelenvirtuose Feb 13 '16 at 16:44
  • Yes, i know. But i am sending this SOAP message to a server, which is not able to process elements (inside a:A1) with prefixes. – mrq Feb 13 '16 at 16:49
  • Then - really - this server must be fixed. Sorry if that doesn't help you, but trying to work around something that is valid is not appropriate in some cases. – Seelenvirtuose Feb 13 '16 at 16:55
  • Yes, i agree. However, fixing server is not possible. – mrq Feb 13 '16 at 17:07

1 Answers1

1

I solved this using custom SOAPHandler and removing prefixes from element in urn:myns/b namespace.

Simplified snippet:

@Override
public boolean handleMessage(SOAPMessageContext context) {

  SOAPBody body = context.getMessage().getSOAPPart().getEnvelope().getBody();

  //do recursivelly, this is just example
  Iterator iter = body.getChildElements();
  while (iter.hasNext()) {
    Object object = iter.next();

    if (object instanceof SOAPElement) {
      SOAPElement element = (SOAPElement) object;

      if("urn:myns/b".equals(element.getNamespaceURI())){
         element.setPrefix("");
      }       
   }
}
mrq
  • 13
  • 3