【问题标题】:Is it possible to see the whole REST request body/payload using a filter是否可以使用过滤器查看整个 REST 请求正文/有效负载
【发布时间】:2017-07-14 00:20:09
【问题描述】:

我想知道是否有一种方法可以使用像 com.sun.jersey.spi.container.ContainerRequestFilter 这样的过滤器来查看整个 REST PUT 请求。这将帮助我们查看请求正文中的特殊字符,这会导致应用程序抛出错误的请求代码 400。

我们尝试使用 UTF-8 字符集,但没有帮助。有没有办法让我们允许像 ^B 这样的特殊字符并在服务中处理它们。

【问题讨论】:

    标签: java rest filter jax-rs


    【解决方案1】:

    我总是在实现 containerRequestFilter 的 myFilter 类中使用以下方法。

    这个方法返回像 {"a":"1","b":"2"} 这样的 jsonString。

    private String getEntityBody(ContainerRequestContext requestContext) {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      InputStream in = requestContext.getEntityStream();
    
      String result = null;
      try {
        ReaderWriter.writeTo(in, out);
    
        byte[] requestEntity = out.toByteArray();
        if (requestEntity.length == 0) {
          result = "";
        } else {
          result = new String(requestEntity, "UTF-8");
        }
        requestContext.setEntityStream(new ByteArrayInputStream(requestEntity));
    
      } catch (IOException e) {
      }
      return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-05
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 2017-01-08
      相关资源
      最近更新 更多