【问题标题】:RESTEasy - Response with duplicated Cache-Control - Wildfly10RESTEasy - 使用重复的 Cache-Control 响应 - Wildfly10
【发布时间】:2016-07-01 06:29:49
【问题描述】:

我有一个带有图像的 GET 响应:

@GET
@Path("{id}/thumbnail")
public Response readThumbnailById(@PathParam("id") String id, @QueryParam("serviceContext") EIServiceContext serviceContext) throws BadRequestRestException {
    try {
        serviceContextServices.setFullContext(serviceContext);
        byte[] thumbnail = documentClientWebServices.readThumbnailById(id);
        CacheControl cc = new CacheControl();
        cc.setMaxAge(2592000);
        cc.setPrivate(true);

        ResponseBuilder builder = Response.ok(thumbnail,"image/png");
        builder.cacheControl(cc);
        return builder.build();
    } catch (XECMException ex) {
        throw new BadRequestRestException(ex);
    } catch (ComponentException ex) {
        XECMRestException e = new XECMRestException(ex);
        return Response.noContent().entity(e.getEiResultStatus()).build();
    }
}

我希望客户端浏览器自动缓存我的图像,

但是图片没有缓存,

Chrome 的响应头给我看:

HTTP/1.1 200 OK
Content-Encoding: gzip
Expires: 0
Cache-Control: no-cache, no-store, must-revalidate
Cache-Control: no-transform, max-age=2592000, private
X-Powered-By: Undertow/1
Server: WildFly/10
Pragma: no-cache
Date: Fri, 01 Jul 2016 05:57:21 GMT
Connection: keep-alive
Content-Type: image/png
Content-Length: 8727

这是因为重复的Cache-Control

如何删除第一个 Cache-Control 条目?

谢谢

【问题讨论】:

  • 我在我的机器上尝试了 RESTEasy(我使用的是 3.0.9 Final),但没有这个问题。您是否有一些 ContainerResponseFilter 可能会在您的回复中添加 Cache-Control 标头?
  • 我找到了一个相关的帖子developer.jboss.org/thread/249410?start=0&tstart=0

标签: java rest caching resteasy


【解决方案1】:

google了很多之后,我发现,一个全局的解决方案:

<subsystem xmlns="urn:jboss:domain:undertow:3.0">
        <buffer-cache name="default"/>
        <server name="default-server">
            <http-listener name="default" socket-binding="http" redirect-socket="https"/>
            <https-listener name="https" security-realm="UndertowRealm" socket-binding="https"/>
            <host name="default-host" alias="localhost">
                <location name="/" handler="welcome-content"/>
                <filter-ref name="server-header"/>
                <filter-ref name="x-powered-by-header"/>
                <filter-ref name="gzipFilter" predicate="exists['%{o,Content-Type}'] or regex[pattern='(?:application/javascript|text/css|text/html|text/xml|application/json)(;.*)?', value=%{o,Content-Type}, full-match=true]"/>
                <filter-ref name="custom-max-age" predicate="exists['%{o,Content-Type}'] or regex[pattern='(?:image/png)(;.*)?', value=%{o,Content-Type}, full-match=true] or path-suffix['.js'] or path-suffix ['.json'] or path-suffix ['.html'] or path-suffix ['.css'] or path-suffix ['.jpg'] or path-suffix ['.jpeg'] or path-suffix ['.png'] or path-suffix ['.gif']"/>
            </host>
        </server>
        <servlet-container name="default" disable-caching-for-secured-pages="false">
            <jsp-config/>
            <websockets/>
        </servlet-container>
        <handlers>
            <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
        </handlers>
        <filters>
            <response-header name="server-header" header-name="Server" header-value="WildFly/10"/>
            <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
            <response-header name="custom-max-age" header-name="Cache-Control" header-value="max-age=86400, public"/>
            <gzip name="gzipFilter"/>
        </filters>
    </subsystem>

关键是:

<servlet-container name="default" disable-caching-for-secured-pages="false">

disable-caching-for-secured-pages之后,没有第一个条目Cache-Control: no-cache, no-store, must-revalidate

【讨论】:

    猜你喜欢
    • 2016-05-07
    • 2022-01-24
    • 2014-05-08
    • 2020-09-15
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2021-04-15
    相关资源
    最近更新 更多