gpt4 book ai didi

hibernate - java.io.IOException : Premature EOF in REST in java using jersey 异常

转载 作者:行者123 更新时间:2023-11-28 22:44:27 25 4
gpt4 key购买 nike

我实现了一个 RESTful Web Service in Java using jersey并在 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 客户端,它面临内存不足的问题。

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

最佳答案

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

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

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

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

另一种方法是“页面”

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

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

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

希望这会引发一些关于不同替代方案的想法。可能不是您问题的确切答案。

关于hibernate - java.io.IOException : Premature EOF in REST in java using jersey 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12335687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com