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);
}
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) {
} catch (Exception ex) {
}
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.
source
share