优化Web服务的XML响应

时间:2009-12-21 09:53:52

标签: java xml web-services namespaces

在我们的网络服务的性能测试中,我们发现响应产生的流量超出了我们的预期。我们正在查询数据库并加载由行和列组成的列表。

列的类型是 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?

3 个答案:

答案 0 :(得分:3)

您是否考虑过以适当透明的方式尝试压缩响应?这可能更容易做到,并且对于所有重复数据都非常有效。

答案 1 :(得分:1)

如果您对网络服务的传输和/或处理效率有疑虑,应考虑启用Fast Infoset

  

Fast Infoset(或FI)是一个   国际标准规定   XML的二进制编码格式   信息集(XML Infoset)作为   替代XML文档   格式。它旨在提供更多   高效的序列化比   基于文本的XML格式。

     

可以将FI视为XML的gzip,   虽然FI旨在优化两者   文件大小和处理   性能,而gzip优化   只有尺寸。

它对大量网络服务的影响是巨大的,我现在在可能的情况下使用它。

Axis2JAX-WS支持它。

答案 2 :(得分:0)

AXIS 1.x的servlet compression filter示例。

  

本指南介绍了如何在Apache Axis中使用SOAP压缩。请求和响应消息都被压缩。要在客户端压缩和解压缩SOAP消息,请使用gzip的Axis扩展。服务器端的对应部分是Servlet过滤器。