EHCache 和 DropWizard:没有返回响应

EHCache and DropWizard: Returning no response

我已成功将 SimplePageCachingFilter 与 DropWizard 集成。

但是,有一件事挡住了我的路。每当我的应用程序中抛出异常时,我的 API 浏览器中都会出现 "Response contains no data",而不是路由到我的 ExceptionMapper。我也碰巧在日志中看到了这个经过。 WARN [2015-02-12 04:06:21,768] net.sf.ehcache.constructs.web.GenericResponseWrapper: Discarding message because this method is deprecated.

传统上我的 ExceptionMapper returns 适当的 Json 响应。

有没有人见过类似的东西?

我会在第 1472 行的 http://grepcode.com/file/repo1.maven.org/maven2/com.sun.jersey/jersey-server/1.18.1/com/sun/jersey/server/impl/application/WebApplicationImpl.java#WebApplicationImpl 中放置一个断点,然后触发您的错误。

那是抛出异常,异常到异常映射器的映射发生在堆栈中稍深一点的捕获中。可能在 ContainerResponse.mapException

希望这能让您深入了解为什么它没有被调用。

仅供参考。我通过在被触发的异常映射器中放置断点并查看调用堆栈来获取该代码。

将调试器附加到 DropWizard 后(根据@CAB 的建议)我发现 CachingFilter 不会写出响应,除非状态代码为 200。

我扩展了 CachingFilter 并覆盖了 doFilter 方法并忽略了状态检查。这似乎解决了我的问题。

@Override
protected void doFilter(final HttpServletRequest request,
                        final HttpServletResponse response, final FilterChain chain) throws Exception {
    if (response.isCommitted()) {
        throw new AlreadyCommittedException("Response already committed before doing buildPage.");
    }
    logRequestHeaders(request);
    PageInfo pageInfo = buildPageInfo(request, response, chain);

    writeResponse(request, response, pageInfo);
}