Cache-Controlの設定

【前提条件】

[環境]

【概要】

JAX-RSでキャッシュのコントロールを行う方法です。

【サンプルコード】

public class ResponseCreator {

    public ResponseCreator(final String urlString) throws Exception {

        final URI uri = new URI(urlString);

        final Response.ResponseBuilder builder = Response.seeOther(uri);

        // キャッシュの設定
        final CacheControl cacheControl = new CacheControl();
        cacheControl.setNoCache(true);
        cacheControl.setMustRevalidate(true);
        cacheControl.setNoStore(true);

        builder.cacheControl(cacheControl);

        response = builder.build();
    }

}

キャッシュコントロールの設定を行うためには
javax.ws.rs.core.CacheControlクラスを使用します。

CacheControlオブジェクトにプロパティを設定し、
javax.ws.rs.core.Response.ResponseBuilder#cacheControlメソッドの
パラメータとして設定します。

設定後、buildメソッドでResponseオブジェクトを作成すると
HTTPレスポンスのヘッダに追記されます。

サンプルコードで返却されるChache-Controlは↓のようになります。

Name Value
Cache-Control no-cache, no-store, no-transform, must-revalidate

【設定メソッドの概要】

各設定メソッドと設定される内容は↓のようになります。

メソッド名 Cache-Controlにセットされるプロパティ デフォルトの設定有無
setMaxAge max-age なし
setMustRevalidate must-revalidate なし
setNoCache no-chache なし
setNoStore no-store なし
setNoTransform no-transform あり
setPrivate private なし
setProxyRevalidate proxy-revalidate なし
setSMaxAge s-maxage なし