CXF + Java:如何删除 xmlns 属性



我正在开发一个必须使用 SOAP Web 服务的应用程序。我使用 CXF 从 Web 服务的 WSDL 创建了存根。

请求的元素之一如下所示:

@XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name="FolderDetailsType", namespace="http://newyorklife.com/services/businessdomain/distributionmanagement/maintenance/maintenancerequestsresponses", propOrder={"any"})
public class FolderDetailsType
  implements Serializable
 {
  private static final long serialVersionUID = -6026937020915831338L;
  @XmlAnyElement
  protected List<Element> any;
 public FolderDetailsType() {}
  public List<Element> getAny()
  {
   if (this.any == null) {
    this.any = new ArrayList();
  }
   return this.any;
   }
 }

在我的适配器类中,我正在创建整个请求,我正在创建名为"businessTypeCode"的元素并将其添加到元素列表中。

FolderDetailsType folderDetails = new FolderDetailsType();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Element businessTypeCode = document.createElement("BusinessTypeCd");
            businessTypeCode.setAttribute("code", requestMetadata.getBusinessTypeCode());
            folderDetails.getAny().add(businessTypeCode);

当 CXF 生成请求 XML 时,如下所示:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Body>
        <ns21:InsertBusinessDocumentRequest xmlns="http://services/businessdomain/distributionmanagement/maintenance/maintenancerequestsresponses" xmlns:ns10="http://schemas/framework/exceptionentry" xmlns:ns11="http://schemas/framework/systemexception" xmlns:ns12="http://schemas/cim/claim/deathclaim" xmlns:ns13="http://schemas/cim/financialcontract/contract" xmlns:ns14="http://schemas/cim/common/name" xmlns:ns15="http://schemas/framework/baserequest" xmlns:ns16="http://schemas/framework/severity" xmlns:ns17="http://schemas/framework/systemexceptionlocation" xmlns:ns18="http://schemas/framework/status" xmlns:ns19="http://schemas/framework/businessexception" xmlns:ns2="http://schemas/framework/baseresponse" xmlns:ns20="http://schemas/framework/fatalexception" xmlns:ns21="http://services/businessdomain/distributionmanagement/maintenance/businessdocumentmaintenance" xmlns:ns3="http://schemas/framework/contextsummary" xmlns:ns4="http://schemas/framework/param" xmlns:ns5="http://schemas/framework/servicestatus" xmlns:ns6="http://schemas/framework/messagetype" xmlns:ns7="http://schemas/framework/businessandservicestatus" xmlns:ns8="http://schemas/framework/businessstatus" xmlns:ns9="http://schemas/framework/serviceexception">
            <AsOfDt>2011-01-01-04:00</AsOfDt>
            <ApplicationInfo>
                <ApplicationId>ABC</ApplicationId>
                <ApplicationVersion>1.0</ApplicationVersion>
            </ApplicationInfo>
            <FolderInfo>
                <FolderDetails>
                    <BusinessTypeCd xmlns="" xmlns:ns22="http://services/businessdomain/distributionmanagement/maintenance/maintenancerequestsresponses" code="J40"/>
                </FolderDetails>
                <FolderName>GY67898</FolderName>
                <FolderTypeCd code="ABC"/>
            </FolderInfo>
            <MIMETypeCd code="pdf"/>
        </ns21:InsertBusinessDocumentRequest>
    </soap:Body>
</soap:Envelope>

如您所见,与 XML 的其他元素不同,BusinessTypeCd 元素有两个 xmlns 标记 - 一个为空,一个带有命名空间:

<BusinessTypeCd xmlns="" xmlns:ns22="http://services/businessdomain/distributionmanagement/maintenance/maintenancerequestsresponses" code="J40"/>

但是我希望元素像其他元素一样生成,例如:

<BusinessTypeCd code="J40"/>

有人可以告诉我如何从业务类型Cd元素中删除xmlns标签吗?任何帮助真的非常感谢。

我找到了解决问题的方法。如果有人登陆这里寻找答案,请在此处发布。我采取了不同的方法。我让 CXF 创建 SOAP 消息,然后使用 CXF 自定义拦截器对消息元素进行更改。

配置 XML:

<!-- CXF Bus Configuration -->
<cxf:bus name="clientBus">
    <cxf:outInterceptors>
        <bean class="com.xxx.xxx.xxx.xxx.CustomMessageInterceptor" />
    </cxf:outInterceptors>
    <cxf:features>
        <cxf:logging/>
    </cxf:features>
</cxf:bus> 

自定义消息拦截器.java

package com.xxx.xxx.xxx.xxx;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomMessageInterceptor extends AbstractPhaseInterceptor<Message> {
    public CustomMessageInterceptor() {
        super(Phase.PRE_STREAM);
        addBefore(SoapPreProtocolOutInterceptor.class.getName());
    }
    private Logger log = LoggerFactory.getLogger(this.getClass());
    public void handleMessage(
            Message message) {
        OutputStream os = message.getContent(OutputStream.class);
        CachedStream cs = new CachedStream();
        message.setContent(OutputStream.class, cs);
        message.getInterceptorChain().doIntercept(message);
        try {
            cs.flush();
            IOUtils.closeQuietly(cs);
            CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);
            String currentEnvelopeMessage = IOUtils.toString(csnew.getInputStream(), "UTF-8");
            csnew.flush();
            IOUtils.closeQuietly(csnew);
            if (log.isDebugEnabled()) {
                log.debug("Outbound message: " + currentEnvelopeMessage);
            }
            String res = changeOutboundMessage(currentEnvelopeMessage);
            if (res != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Outbound message has been changed: " + res);
                }
            }
            res = res != null ? res : currentEnvelopeMessage;
            InputStream replaceInStream = IOUtils.toInputStream(res, "UTF-8");
            IOUtils.copy(replaceInStream, os);
            replaceInStream.close();
            IOUtils.closeQuietly(replaceInStream);
            os.flush();
            message.setContent(OutputStream.class, os);
            IOUtils.closeQuietly(os);
        }
        catch (IOException ioe) {
            log.error("Unable to perform change.", ioe);
            throw new RuntimeException(ioe);
        }
    }
    protected String changeOutboundMessage(
            String currentEnvelope) {
        currentEnvelope = currentEnvelope.replace("<ClruInsert xmlns="" xmlns:ns22="http://services/businessdomain/distributionmanagement/maintenance/maintenancerequestsresponses">", "<ClruInsert>");
        currentEnvelope = currentEnvelope.replace("<BusinessTypeCd xmlns="" xmlns:ns22="http://services/businessdomain/distributionmanagement/maintenance/maintenancerequestsresponses"", "<BusinessTypeCd");
        return currentEnvelope;     
    }
    private class CachedStream extends CachedOutputStream {
        public CachedStream() {
            super();
        }
    }
}

它就像一个魅力! :)

更改

Element businessTypeCode = document.createElement("BusinessTypeCd");

Element businessTypeCode = document.createElementNS(
  "http://services/businessdomain/distributionmanagement/maintenance/maintenancerequestsresponses", 
  "BusinessTypeCd");

并参阅 https://docs.oracle.com/javase/8/docs/api/org/w3c/dom/Document.html#createElementNS-java.lang.String-java.lang.String- 了解更多详情。

相关内容

  • 没有找到相关文章

最新更新