属性中的JAX-WS命名空间,而不是前缀

时间:2016-02-13 16:41:28

标签: java xml soap jax-ws jax-ws-customization

是否可以在JAX-WS中生成xmlns属性而不是前缀?

示例:来自包myns.a的对象A包含来自包myns.b的一些对象B1,B2。生成的SOAP消息:

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

但是,我需要以这种方式生成它(因此应删除前缀b并且包myns.b中的所有对象都应具有xmlns属性):

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

有一种简单的方法,如何处理?例如,在package-info.java级别?

1 个答案:

答案 0 :(得分:1)

我使用自定义SOAPHandler解决了这个问题,并从urn:myns / b命名空间中的元素中删除了前缀。

简化代码段:

@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("");
      }       
   }
}