Java 使用HttpServletRequest读取POST表单数据

Java 使用HttpServletRequest读取POST表单数据,java,servlets,http-post,jax-rs,Java,Servlets,Http Post,Jax Rs,几天来,我一直在尝试读取表单中发布的数据,内容类型为application/x-www-Form-urlencoded 根据,通过POST请求和application/x-www-form-urlencoded内容类型发送到服务器的数据如下发送到服务器: POST / HTTP/1.1 Host: foo.com Content-Type: application/x-www-form-urlencoded Content-Length: 13 say=Hi&to=Mom 我想用Ht

几天来,我一直在尝试读取表单中发布的数据,内容类型为
application/x-www-Form-urlencoded

根据,通过POST请求和
application/x-www-form-urlencoded
内容类型发送到服务器的数据如下发送到服务器:

POST / HTTP/1.1
Host: foo.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 13

say=Hi&to=Mom
我想用
HttpServletRequest
读取
say
to
的键和值

以下是我的尝试:

@POST
@Path("/webhook/{psp_code}")    
public Response notifyPSP(@Context HttpServletRequest httpServletRequest,
  @PathParam("psp_code") String pspCode) {

    String payload = getRequestBody( httpServletRequest );
    Map<String, List<String>> formData = getFormData( httpServletRequest );

    log.info( "Payload {}", payload);  
    log.info( "Form Data {}", formData );
}

private String getRequestBody(HttpServletRequest request) {
    String content;

    try
    {
        ServletInputStream inputStream = request.getInputStream();
        content = IOUtils.toString( inputStream );
    }
    catch ( IOException e )
    {
        content = null;
    }
    return content;
}

private Map<String, List<String>> getFormData(HttpServletRequest request) {
    Map<String, String[]> parameterMap = request.getParameterMap();
    Map<String, List<String>> collect = parameterMap.entrySet().stream().collect( Collectors.toMap( entry -> entry.getKey(), entry -> Arrays.asList( entry.getValue() ) ) );
    return collect;
}

也许我在这里遗漏了一些东西,但我如何才能读取这些值呢

您可以尝试记录parameterMap的值吗。因为我希望collect变量有问题。为了测试,我删除了collect和通过
getParameterMap
返回的原始值,结果仍然相同
Payload
Form Data {}