title: How to cache a InputStreamResource In RestController?
body: <p>I have a servlet that returns an image as <code>InputStreamResource</code>. There are approx 50 static images that are to be returned based on some <code>get query</code> parameters.</p>
  
  <p>For not having to look up each of those images every time it is requested (which is very often), I'd like to cache those images responses.</p>
  
  <pre><code>@RestController
  public class MyRestController {
      //code is just example; may be any number of parameters
      @RequestMapping("/{code}")
      @Cachable("code.cache")
      public ResponseEntity&lt;InputStreamResource&gt; getCodeLogo(@PathVariable("code") String code) {
          FileSystemResource file = new FileSystemResource("d:/images/" + code + ".jpg");
          return ResponseEntity.ok()
              .contentType("image/jpg")
              .lastModified(file.lastModified())
              .contentLength(file.contentLength())
              .body(new InputStreamResource(file.getInputStream()));
  
      }
  }
  </code></pre>
  
  <p>When using the <code>@Cacheable</code> annotation (no matter if directly on the <code>RestMapping</code> method or refactored to an external service), I_'m getting the following exception:</p>
  
  <pre><code>cause: java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times - error: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times
  org.springframework.core.io.InputStreamResource.getInputStream(InputStreamResource.java:96)
  org.springframework.http.converter.ResourceHttpMessageConverter.writeInternal(ResourceHttpMessageConverter.java:100)
  org.springframework.http.converter.ResourceHttpMessageConverter.writeInternal(ResourceHttpMessageConverter.java:47)
  org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:195)
  org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:238)
  org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:183)
  org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
  org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126)
  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)
  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)
  </code></pre>
  
  <p><strong>Question</strong>: how can I then cache the <code>ResponseEntity</code> of type <code>InputStreamResource</code> at all?</p>