JERSEY: Trace errors: java.lang.IllegalStateException: Entity input stream is already closed

I work with Jersey 2.x.

Below is my controller

@GET
@Path("{id}")
@Produces("application/json")
public Response getUser(@PathParam("id") int userId, @Context ContainerRequestContext containerRequestContext) {


        ContainerRequestContext requestContext = logRequest(containerRequestContext);

        //To further operations using requestContext

}

Below is my method inside one controller class to write a request

private ContainerRequestContext logRequest(ContainerRequestContext requestContext) {

        JSONObject requestData = new JSONObject();
        try {

            Map bm = null;
            ContainerRequest cr = (ContainerRequest) requestContext;

            cr.bufferEntity();

            bm = cr.readEntity(Map.class);

            if (!String.valueOf(bm).equalsIgnoreCase("null")) {
                requestData.put("parameters", bm.toString());
            }

            requestData.put("uri", requestContext.getUriInfo().getRequestUri());
            requestData.put("ClientIp", requestContext.getProperty("requesterIp"));
            requestContext.setProperty("requestData", requestData);

        } catch (IOException | NumberFormatException | ProcessingException ex) {
            //Logging

        } catch (Exception ex) {
            //Logging
        }
        return requestContext;
    }

As you can see, I use bufferEntity()ContainerRequestContext several times to read.

However, when I deploy my API on my server.

I ran into this error:

Error Trace : java.lang.IllegalStateException: Entity input stream has already been closed.

What I'm doing wrong here. I will be grateful if anyone can explain this behavior to me.

+2
source share

Source: https://habr.com/ru/post/1609304/


All Articles