spring boot - Httpcache on springboot -
i have springboot application requests image url display on browser. want cache response using cache-control header.
i use responseentity , set header etag. checked response header in browser , shows :
cache-control:"max-age=31536000, public" content-type:"image/jpeg;charset=utf-8" etag:"db577053a18fa88f62293fbf1bd4b1ee" my request have if-none-match header. but, 200 status instead of 304.
here's code
@requestmapping(value = "/getimage", method=requestmethod.get) public responseentity<byte[]> getimage() throws exception { string url = "www.example.com/image.jpeg"; string etag = getetag(url); httpheaders headers = new httpheaders(); headers.setcontenttype(new mediatype("image", "jpeg")); headers.add("cache-control", "max-age=31536000, public"); headers.add("etag", etag); url imageurl = new url(url); inputstream = imageurl.openstream(); bufferedimage imbuff = imageio.read(is); bytearrayoutputstream baos = new bytearrayoutputstream(); imageio.write(imbuff, "jpeg", baos); byte[] image = baos.tobytearray(); return new responseentity<byte[]>(image, headers, httpstatus.ok); } can me?
update
i tried using method described in unable cache images served spring mvc code become :
@requestmapping(value = "/getimage", method=requestmethod.get) public responseentity<byte[]> getimage() throws exception { string url = "www.example.com/image.jpeg"; string etag = getetag(url); url imageurl = new url(url); httpurlconnection httpcon = (httpurlconnection)imageurl.openconnection(); long lastmodified = httpcon.getlastmodified(); httpheaders headers = new httpheaders(); headers.setcontenttype(new mediatype("image", "jpeg")); headers.add("cache-control", "max-age=31536000, public"); headers.add("etag", etag); headers.add("last-modified", new date(lastmodified).tostring()); if (webrequest.checknotmodified(etag)) { return null; } inputstream = imageurl.openstream(); bufferedimage imbuff = imageio.read(is); bytearrayoutputstream baos = new bytearrayoutputstream(); imageio.write(imbuff, "jpeg", baos); byte[] image = baos.tobytearray(); return new responseentity<byte[]>(image, headers, httpstatus.ok); } but 304 status code if change url. checked webrequest.checkisnotmodified(...) either etag , last-modified , return true. did wrong here?
i ended changing code. instead of using webrequest.checknotmodified(etag) manually check last-modified , etag resource (which s3) , compare if-modified-since , if-none-match in request header.
besides, move related http caching in filter.
Comments
Post a Comment