[英]Optimizing XML response of web service
在我們的Web服務的性能測試中,我們發現響應產生的流量超出了我們的預期。 我們正在查詢數據庫並加載由行和列組成的列表。
列的類型是AnyType,因此響應中需要有類型信息。 因此,Web服務引擎(Axis2或JAXWS) 多次添加了大量命名空間信息 。 請參閱以下示例響應:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns3:loadListResponse xmlns:ns3="http://example.com/test/service-types-1.0"
xmlns:ns2="http://example.com/lists/lists-types-1.0" >
<ns3:value>
<ns2:row>
<ns2:column xsi:type="xs:int" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">12345</ns2:column>
<ns2:column xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">XYZ</ns2:column>
<ns2:column xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns2:column xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">ABC</ns2:column>
</ns2:row>
<ns2:row>
<ns2:column xsi:type="xs:int" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">32345</ns2:column>
<ns2:column xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">OPC</ns2:column>
<ns2:column xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns2:column xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">QWE</ns2:column>
</ns2:row>
.
.
.
</ns3:value>
</ns3:loadListResponse>
</soapenv:Body>
</soapenv:Envelope>
我想通過在頂部添加所需的命名空間並從每個列中刪除它們來優化此XML響應(通常每行大約有30列)。 結果應如下所示:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soapenv:Body>
<ns3:loadListResponse xmlns:ns3="http://example.com/test/service-types-1.0"
xmlns:ns2="http://example.com/lists/lists-types-1.0" >
<ns3:value>
<ns2:row>
<ns2:column xsi:type="xs:int" >12345</ns2:column>
<ns2:column xsi:type="xs:string" >XYZ</ns2:column>
<ns2:column xsi:nil="true" />
<ns2:column xsi:type="xs:string" >ABC</ns2:column>
</ns2:row>
<ns2:row>
<ns2:column xsi:type="xs:int" >32345</ns2:column>
<ns2:column xsi:type="xs:string" >OPC</ns2:column>
<ns2:column xsi:nil="true" />
<ns2:column xsi:type="xs:string" >QWE</ns2:column>
</ns2:row>
.
.
.
</ns3:value>
</ns3:loadListResponse>
</soapenv:Body>
</soapenv:Envelope>
你會怎么做那樣的事情?
有沒有辦法告訴Axis2或JAXWS這樣做?
或者我是否需要手動操作生成的XML?
您是否考慮過以適當透明的方式嘗試壓縮響應? 這可能更容易做到,並且對於所有重復數據都非常有效。
如果您擔心Web服務的傳輸和/或處理效率,您應該考慮啟用Fast Infoset :
Fast Infoset(或FI)是一種國際標准,它為XML信息集(XML Infoset)指定二進制編碼格式,以替代XML文檔格式。 它旨在提供比基於文本的XML格式更高效的序列化。
可以將FI視為XML的gzip,盡管FI旨在優化文檔大小和處理性能,而gzip僅優化大小。
它對大量Web服務的影響是巨大的,我現在在可能的情況下使用它。
AXIS 1.x的servlet壓縮過濾器示例。
本指南介紹了如何在Apache Axis中使用SOAP壓縮。 請求和響應消息都被壓縮。 要在客戶端壓縮和解壓縮SOAP消息,請使用gzip的Axis擴展。 服務器端的對應物是Servlet過濾器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.