How to set content-length while returning user defined objects

2.1k Views Asked by At

I have Jersey rest services running on tomcat. I'm returning a Response object for a rest call, but the entity of the payload is a list of objects of a class that i defined. How can i set the content-length in the response header for this List

    @Path("/somePath")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response myRestAPI(@CookieParam(value = "value") String cookieValue) {
        /*..
        * some business logic here
        */
        List<MyObject> myObjects = getMyObjectList();
        return Response.ok(myObjects).cookie(createCookie(value)).build();
    }

I intend on setting the content-length header in the Response object as

return Response.ok(myObjects).header("content-length", length).cookie(createCookie(value)).build();

How can i find the length of the list? Thanks in advance

1

There are 1 best solutions below

2
On

I guess the Content-Length header should be automatically included. In my case using RestEasy (not Jersey) it is.

Can you confirm that the header is not included, e.g. using curl or Firebug?

Finally, just to be 100% sure: Content-Length is the length of the HTTP content (the body, see here), not the size of the list, as returned by List.size().