小编典典

java.io.IOException:使用jersey的Java REST中的过早EOF

tomcat

使用jersey在Java中实现RESTful
Web
Service
并在上运行它Tomcat7

我有此查询以获取任务列表:

@GET
@Path("/users/{username}/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response taskList(@PathParam("username") String username) {      
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Task> tasks = null;
        try { 
            tasks = (List<Task>)session.createQuery("from Task as t where t.user.username = :name order by t.project.key ASC, t.deliverable.key ASC, t.activity.key ASC, t.start ASC").setString("name", username).list();   
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return Response.ok().entity(tasks).build();
    }

我用jersey客户端测试它,例如:

try {
    response = service.path("task/users/are/").type("application/json").get(ClientResponse.class);
    s = response.getEntity(String.class);
    System.out.println(s);
} catch (Exception e) {
    e.printStackTrace();
}

结果示例:

[{"id":5369,"start":1331428800000,"end":1331428800000,"description":"","started":false,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"activity":{"id":102,"key":"d","name":"Development","description":"Usually this.","cost":690.0,"orderIndex":2,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},"deliverable":{"id":1282,"description":"andra saker","key":"andra","name":"Other","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":{"id":1266,"description":"","key":"other","name":"Other","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":null,"user":null,"orderIndex":0,"done":false,"minEstimate":null,"maxEstimate":null,"task_id":0},"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":3,"done":false,"minEstimate":10.0,"maxEstimate":30.0,"task_id":0},"link":""},{"id":5324,"start":1331254080000,"end":1331254800000,"description":"","started":false,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"activity":{"id":102,"key":"d","name":"Development","description":"Usually this.","cost":690.0,"orderIndex":2,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},"deliverable":{"id":1274,"description":"","key":"login","name":"Login","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":{"id":1273,"description":"","key":"client","name":"Android client","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":null,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":2,"done":false,"minEstimate":null,"maxEstimate":null,"task_id":0},"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":1,"done":false,"minEstimate":15.0,"maxEstimate":30.0,"task_id":0},"link":""},{"id":7901,"start":1338678900000,"end":1338678900000,"description":"","started":false,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"activity":{"id":101,"key":"c","name":"Consulting","description":"Just in special cases, authorized by Rüdiger after discussion with customer.","cost":990.0,"orderIndex":3,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},"deliverable":{"id":1278,"description":"Stagtistic check","key":"statistic","name":"Statistics","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":{"id":1273,"description":"","key":"client","name":"Android client","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":null,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":2,"done":false,"minEstimate":null,"maxEstimate":null,"task_id":0},"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":5,"done":false,"minEstimate":20.0,"maxEstimate":40.0,"task_id":0},"link":""},{"id":10441,"start":1347134220000,"end":1347135120000,"description":"hello","started":false,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"activity":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"deliverable":{"id":1278,"description":"Stagtistic check","key":"statistic","name":"Statistics","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":{"id":1273,"description":"","key":"client","name":"Android client","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":null,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":2,"done":false,"minEstimate":null,"maxEstimate":null,"task_id":0},"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":5,"done":false,"minEstimate":20.0,"maxEstimate":40.0,"task_id":0},"link":""},{"id":6259,"start":1334170080000,"end":1334170140000,"description":"hi","started":false,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"activity":{"id":102,"key":"d","name":"Development","description":"Usually this.","cost":690.0,"orderIndex":2,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},"deliverable":{"id":1276,"description":"","key":"task","name":"Uppgifter","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":{"id":1273,"description":"","key":"client","name":"Android client","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":null,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":2,"done":false,"minEstimate":null,"maxEstimate":null,"task_id":0},"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":3,"done":false,"minEstimate":15.0,"maxEstimate":30.0,"task_id":0},"link":""},{"id":7900,"start":1338678600000,"end":1338678780000,"description":"","started":false,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"activity":{"id":102,"key":"d","name":"Development","description":"Usually this.","cost":690.0,"orderIndex":2,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},"deliverable":{"id":1276,"description":"","key":"task","name":"Uppgifter","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":{"id":1273,"description":"","key":"client","name":"Android client","project":{"id":115,"key":"are","name":"Master Thesis","description":"Android client for Timelog","date":1328396400000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":null,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":2,"done":false,"minEstimate":null,"maxEstimate":null,"task_id":0},"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"orderIndex":3,"done":false,"minEstimate":15.0,"maxEstimate":30.0,"task_id":0},"link":""},{"id":6258,"start":1334170020000,"end":1334170080000,"description":"Develop","started":false,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"project":{"id":109,"key":"mre","name":"Master Thesis Ali Rezaei","description":"Timelog Android","date":1328223600000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"activity":{"id":102,"key":"d","name":"Development","description":"Usually this.","cost":690.0,"orderIndex":2,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},"deliverable":{"id":1495,"description":"Develop statistic","key":"3.2","name":"statistic","project":{"id":109,"key":"mre","name":"Master Thesis Ali Rezaei","description":"Timelog Android","date":1328223600000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":{"id":1240,"description":"Developing","key":"3","name":"Android Client","project":{"id":109,"key":"mre","name":"Master Thesis Ali Rezaei","description":"Timelog Android","date":1328223600000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":null,"user":null,"orderIndex":2,"done":false,"minEstimate":0.0,"maxEstimate":0.0,"task_id":0},"user":null,"orderIndex":2,"done":false,"minEstimate":null,"maxEstimate":null,"task_id":0},"link":""},{"id":10442,"start":1347134640000,"end":1347135540000,"description":"hello","started":false,"user":{"id":28,"username":"are","hash":"$2a$10$G0e7cjXtn60XmAhUx8JpNOYk.EwpIEwDvc2mBaNsTGqQe1tgIZAxy"},"project":{"id":109,"key":"mre","name":"Master Thesis Ali Rezaei","description":"Timelog Android","date":1328223600000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"activity":{"id":108,"key":"v","name":"Vacation/Holidays","description":"Planned vacation (counting agains the 25 days)","cost":300.0,"orderIndex":5,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},"deliverable":{"id":1237,"description":"","key":"o","name":"Other","project":{"id":109,"key":"mre","name":"Master Thesis Ali Rezaei","description":"Timelog Android","date":1328223600000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":{"id":1236,"description":"","key":"other","name":"Other","project":{"id":109,"key":"mre","name":"Master Thesis Ali Rezaei","description":"Timelog Android","date":1328223600000,"category":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},"valueType":0,"fixValue":0.0,"done":0,"task_id":0},"parent":null,"user":null,"orderIndex":0,"done":false,"minEstimate":null,"maxEstimate":null,"task_id":0},"user":null,"orderIndex":2,"done":true,"minEstimate":null,"maxEstimate":null,"task_id":0},"link":""}]

它非常适合此用户( )和除其中一个用户之外的所有其他用户。

该用户在数据库中具有 大量 任务,因此出现了此错误:

Caused by: java.io.IOException: Premature EOF
    at sun.net.www.http.ChunkedInputStream.readAheadBlocking(Unknown Source)
    at sun.net.www.http.ChunkedInputStream.readAhead(Unknown Source)
    at sun.net.www.http.ChunkedInputStream.read(Unknown Source)
    at java.io.FilterInputStream.read(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.Reader.read(Unknown Source)
    at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:171)
    at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:157)
    at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.readFromAsString(AbstractMessageReaderWriterProvider.java:114)
    at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:73)
    at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:58)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:554)
    ... 2 more

感谢您的帮助。


附加物

我发现问题不在服务器端。它在客户端。我有一个面临内存不足问题的Android客户端。

问题是我该如何在客户端解决此问题。你有什么主意吗?


阅读 347

收藏
2020-06-16

共1个答案

小编典典

如果返回的项目数量确实 很大,
则另一个选择是将数据分页回客户端。如果列表足够多,那么它可能甚至对用户和界面都没有用(不确定您的使用经验)。例如,查看StackOverflow-
每天都有大量帖子,但是界面(可能还有后端查询模式)可以分页数据。

简单的分页方法是“更多”模式

/users/{username}/tasks?top=n

如果以25开始,那么可能还有更多链接可以拉到前50位等等。有点像ios邮件和其他类似的应用程序。

另一种方法是“页面”

/users/{username}/tasks?page=1

有趣的问题是您是否希望该集合稳定。换句话说,该用户的任务正在更改,因此转到第2页可能会跳过项目或显示第1页中的项目-除非您为该用户保留“查询”状态-
可能存储结果集(可以是ID )在数据库,中间层(有问题的多个AT)或在客户端(返回所有ID并具有客户端页面列/数据)中的一种-类似于虚拟列表视图。

当您使用庞大的数据集时,仅返回所有数据通常不是正确的答案,然后开始使分页等方法变得复杂。

希望能引起人们对不同选择的思考。可能不是您问题的确切答案。

2020-06-16