【问题标题】:Retrieve the entity in Resteasy PreProcessInterceptor在 Resteasy PreProcessInterceptor 中检索实体
【发布时间】:2015-08-27 07:31:01
【问题描述】:

我正在使用 resteasy PreProcessInterceptor 记录请求,但找不到在 POST 请求中获取 entityjson 对象的方法。是否可以在此级别获取实体或json 对象? 我正在使用 Resteasy 2.3.6

【问题讨论】:

  • @lefloh 谢谢,但我使用的是 resteasy 2.3.6 并且 ContainerRequestFilter 不可用。
  • 代码很相似。我添加了一个修改后的示例。

标签: rest jax-rs httprequest resteasy interceptor


【解决方案1】:

您可以从request.getInputStream() 获取发布的实体,但请注意InputStream 不能被读取两次。一种简单(但可能不是最高效)的方法是复制InputStream

@Provider
public class LoggingInterceptor implements PreProcessInterceptor {

    private static final Logger LOG = LoggerFactory.getLogger(LoggingInterceptor.class);

    @Override
    public ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws Failure, WebApplicationException {
        if ("POST".equals(request.getHttpMethod()) && request.getInputStream() != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                IOUtils.copy(request.getInputStream(), baos);
                byte[] bytes = baos.toByteArray();
                LOG.info("Posted: " + new String(bytes, "UTF-8"));
                request.setInputStream(new ByteArrayInputStream(bytes));
            } catch (IOException ex) {
                throw new WebApplicationException(ex);
            }
        }
        return null;
    }

}

如果您使用的是 Resteasy 3.x,您应该use a ContainerRequestFilter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-09
    • 2015-01-25
    • 1970-01-01
    • 2013-12-06
    • 2020-01-16
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    相关资源
    最近更新 更多