有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

属性中的java JAXWS命名空间,而不是前缀

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

示例:包myns中的对象A。a包含来自包myns的一些对象B1、B2。b、 生成的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>

有没有简单的方法,如何处理?例如关于包信息。java级别


共 (1) 个答案

  1. # 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("");
          }       
       }
    }