无法在Jersey中获取POST请求的有效负载

时间:2016-01-19 01:27:12

标签: java http servlets java-ee post

我需要恢复表单POST请求的整个有效负载,然后将其发送回接收方(PayPal要求将其作为验证程序)。我尝试了几种方法,但我总是得到相同的警告,没有有效载荷:

Warning:   A servlet request to the URI http://localhost:8080/ipnlistener contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.

以下是监听器的代码

@POST
@Path("/ipnlistener")
public Response testIpn(String t) {  
   System.out.println(t);
   return Response.ok().build();
}

我尝试添加不同的Consumes注释但没有任何变化。当然,使用FormParam注释,我能够检索单个字段,但我确实需要整个请求。

我也试过以下

public Response testIpn(@Context HttpServletRequest req) throws IOException {  
    BufferedReader reader= req.getReader();
    String sCurrentLine;
    while ((sCurrentLine = reader.readLine()) != null) {
        System.out.println(sCurrentLine);
    }

   return Response.ok().build();
}

但我收到了错误

getInputStream() has already been called for this request

1 个答案:

答案 0 :(得分:0)

我认为您可以将以下代码添加到您的文件中:

 @Context  
 private HttpServletRequest request; 

HttpServletRequest包含请求的全部内容!