简体   繁体   English

Jersey Jax-RS的内存错误

[英]Memory error with Jersey Jax-RS

I am returning a large amount of data via a rest service. 我通过休息服务返回大量数据。 The query returns about 200,000 rows of data then gets converted to XML. 该查询返回大约200,000行数据,然后转换为XML。 When I run this service in IE8 I get an error that "there is not enough storage to complete this operation". 当我在IE8中运行此服务时,出现“没有足够的存储空间来完成此操作”的错误。

Is there any best practice for returning a large amout of data this way? 有这种方式返回大量数据的最佳做法吗?

The database run the query in about 5 seconds, so I'm guessing that the problem is JAXB converting it to xml. 数据库在大约5秒钟内运行查询,因此我猜测问题是JAXB将其转换为xml。

Does anyone have any ideas to improve this? 有没有人有任何想法来改善这个?

The Problem 问题

I'm assuming that you are using JPA to get your data as objects that can be handled by JAXB. 我假设您使用JPA将数据作为可由JAXB处理的对象。 If this is the case the JPA objects may be using lazy loading meaning that a query may not realize all the data at once. 如果是这种情况,则JPA对象可能正在使用延迟加载,这意味着查询可能无法一次实现所有数据。 However as the JAXB implementation traverses the object graph, more and more of it is brought into memory until you run out. 但是,当JAXB实现遍历对象图时,越来越多的内容会被带入内存,直到用完为止。

Option #1 - Provide Data in Chunks 选项#1 - 以块为单位提供数据

One approach is to return your data in chunks and offer a URI like the one below: 一种方法是以块的形式返回数据,并提供如下所示的URI:

These parameters tie in very nicely to JPA query settings: 这些参数与JPA查询设置非常吻合:

namedQuery.setFirstResult(10);
namedQuery.setMaxResults(100);

Option #2 - Provide Links to Get More Data 选项#2 - 提供获取更多数据的链接

Alternatively, instead of including all the data you can provide links to get more. 或者,您可以提供链接以获取更多信息,而不是包含所有数据。 For example instead of: 例如,而不是:

<purchase-order>
    <product id="1">
        <name>...</name>
        <price>...</price>
        ...
    </product>
    <product id="2">
        <name>...</name>
        <price>...</price>
        ...
    </product>
    ...
</purchase-order>

You could return the following, the client can then request details on products using the provided URI. 您可以返回以下内容,然后客户端可以使用提供的URI请求有关产品的详细信息。 You can map this in JAXB using an XmlAdapter. 您可以使用XmlAdapter在JAXB中映射它。

<purchase-order>
    <product>http://www.example.com/products/1</product>
    <product>http://www.example.com/products/2</product>
    ...
</purchase-order>

For more information on XmlAdapter see: 有关XmlAdapter的更多信息,请参阅:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM