Webservice Restfull + Jersey + Tomcat 9 return from Json without content-length

0

I'm developing a Webservice application with restfull + Jersey and Tomcat 9. In this system I need to return a Json from a class (Product) to an android application and use contentLength (android) to retrieve the file size and use in progress bar. But the webservice return does not contain the content-length in the Response Header.

The code that generates the json return is:

 @Path("Estoque")
 public class controleestoqueWS {

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("getlistaproduto")
  public String getListaProduto() {
     List<Produto> produto;
     produto = ProdutoDAO.listarProduto();
     Gson g = new Gson();
     return g.toJson(produto);
  }
 }    

In Response Headers I have this return:

   Content-Encoding gzip
   Content-Type application/json
   Date Thu, 01 Nov 2018 20:28:45 GMT
   Transfer-Encoding    chunked
   vary accept-encoding

When I change the codes to:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("getlistaproduto")
public Response getListaProduto() {
    List<Produto> produto;
    produto = ProdutoDAO.listarProduto();
    Gson g = new Gson();
    int var = g.toJson(produto).length();
    return Response.ok(g.toJson(produto)).header("X-Content-Length", var).build();        
}

@Provider
class HeaderFilter implements ContainerResponseFilter {
@Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        String contentLength = responseContext.getHeaderString("X-Content-Length");
        if (contentLength != null) {
            responseContext.getHeaders().remove("Transfer-Encoding");
            responseContext.getHeaders().remove("X-Content-Length");
            responseContext.getHeaders().putSingle("Content-Length", contentLength);
        }    
    }
}  

The return of JSON displays "SyntaxError: JSON.parse: unterminated string at line 1 column 1929074 of the JSON data" and does not come complete. But Response Headers correctly displays Content-Length:

  Content-Length    1929133
  Content-Type  application/json
  Date  Sat, 03 Nov 2018 11:33:43 GMT
  Server    Apache-Coyote/1.1

When the file size returns the JSON has error. Is there a way to fix this? Thanks

    
asked by anonymous 03.11.2018 / 12:47

0 answers