0

使用 jersey 在 Java 中实现了一个 RESTful Web 服务并在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();
    }

我用球衣客户测试它:

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 ) 和所有其他用户,除了其中一个用户。

该用户在数据库中有大量任务,所以我收到了这个错误:

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 客户端,它面临内存不足的问题。

问题是我怎样才能在我的客户端解决这个问题。你有什么主意吗?

4

2 回答 2

2

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

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

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

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

另一种方法是“页面”

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

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

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

希望这能引发对不同选择的一些想法。可能不是您问题的确切答案。

于 2012-09-09T22:25:54.123 回答
0

问题在于返回的数据量很大。尝试下面的代码从响应中读取数据。

    InputStream is = response.getEntityInputStream(); 
    BufferedReader r = new BufferedReader(new InputStreamReader(is)); 
    int more = 1;
    StringBuilder responseString = new StringBuilder();
    char[] cbuf = new char[5000];
    while (more > 0 ) {
        more = r.read(cbuf, 0, 5000);
        responseString.append(cbuf);
    }
    System.out.println(responseString.toString());
于 2012-09-09T16:25:34.503 回答