java.io.IOException:使用jersey的Java REST中的过早EOF [英] java.io.IOException: Premature EOF in REST in java using jersey

查看:81
本文介绍了java.io.IOException:使用jersey的Java REST中的过早EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I implement a RESTful Web Service in Java using jersey and run it on Tomcat7.

我有这个查询来获取任务列表:

I have this query to get list of tasks :

@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();
    }

我用球衣客户端进行测试,例如:

I test it with jersey client like :

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":""}]

此功能非常适合此用户( are )和除其中一个用户之外的所有其他用户.

It works perfectly for this user (are) and all other users except one on them.

该用户在数据库中具有巨大的任务,所以出现了这个错误:

That user has enormous number of tasks in the database, so I got this error :

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客户端.

I found out that the problem is not in the server side. it is in the client side.I have an Android client which faces with out of memory problem.

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

The thing is how can I solve this problem in my client side. Do you have any idea?

推荐答案

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

If the number of items coming back is truly enormous one other option is to page data back to a client. If the list is enormous enough, it might not even be useful to the user and interface (not sure what your experience is). For example, look at StackOverflow - there's an enormous number of posts everyday but the interface (and likely the backend querying patterns) page the data.

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

The simple paging approach is the "more" pattern"

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

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

If it starts off with 25, there could be a more link which pulls top 50 etc... Kind of like ios mail and other similar apps.

另一种方法是页面"

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

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

The interesting question there is whether you want the set to be stable. In other words, the tasks for that user are changing so going to page 2 could skip items or show ones that were in page 1 - unless you persist state for that users 'query' - possible to store the results set (could just be IDs) in the DB, middle tier (problematic multiple ATs) or in the client (return all IDs and have the client page columns/data) - kind of like a virtual listview.

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

When you're working with enormous datasets, just returning all the data is often not the right answer and then it start complicating your approach with paging etc...

希望引发关于不同选择的一些想法.可能不是您问题的确切答案.

Hope that sparks some thoughts on different alternatives. Probably not an exact answer to your problem.

这篇关于java.io.IOException:使用jersey的Java REST中的过早EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆