title: how to add cache control headers to image served by spring mvc
body: <p>I'm trying to implement a binary content controller in Spring MVC.</p>
  
  <p>It's working okay but I want to add caching control headers to the response.</p>
  
  <p>I checked this related question: <a href="https://stackoverflow.com/questions/17821518/unable-to-cache-images-served-by-spring-mvc">Unable to cache images served by Spring MVC</a></p>
  
  <p>But it's using a different method. I wanted to use this requestMapping - produces annotation. Here's what I have so far, but I'm not sure how to set the response headers with the cache control elements.</p>
  
  <pre><code>@RequestMapping(value="/binaries/**", method = RequestMethod.GET, produces={MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.IMAGE_GIF_VALUE,
   MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE})
  public @ResponseBody byte[] serveResource(WebRequest webRequest, String uri) throws IOException {
      String path = (String)request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE );
  
      BinaryFile bf = binaryService.findByUri(path.replaceFirst("/binaries", ""));
      if (webRequest.checkNotModified(bf.getLastModifiedDate().toDate().getTime()))
      {
          return null;
      };
      return bf.getResource();
  }
  </code></pre>