title: HttpMediaTypeNotAcceptableException: Could not find acceptable representation spring mvc
body: <p>This is driving me crazy! I'm trying to serve a JPG image. I am sure this method was working fine the other day, so I don't know what's changed. I've tried many different things to get it to work but I can't seem to get past the exception.</p>
  
  <p>Basically I'm trying to serve an image from the database. </p>
  
  <p>I thought maybe the actual bytes are corrupt so I wrote them to a file, and checked the file content. Just in Finder on Mac, the file in the temp directory looks fine in the preview application so I'm pretty sure it's not the content itself causing the problem.</p>
  
  <p>This is the controller method:</p>
  
  <pre><code>@RequestMapping(value="/binaries/**", method = RequestMethod.GET, produces={MediaType.APPLICATION_JSON_VALUE, MediaType.IMAGE_GIF_VALUE,
              MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE, "application/javascript"})
      public @ResponseBody
      ResponseEntity&lt;byte[]&gt; serveResource(WebRequest webRequest, HttpServletResponse response, String uri) throws IOException {
          String path = (String)request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE );
  
          BinaryFile bf = binaryService.findByUri(path);
          String tmpdir = System.getProperty("java.io.tmpdir");
          File dest = new File(tmpdir + File.separator + bf.getFileName());
          FileUtils.writeByteArrayToFile(dest, bf.getResource());
          logger.debug("file written: " + dest.getAbsolutePath());
  //        response.addHeader("Cache-Control", "public, max-age=3600");
  
          if (webRequest.checkNotModified(bf.getLastModifiedDate().toDate().getTime()))
          {
              return null;
          };
          return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(bf.getResource());
      }
  </code></pre>
  
  <p>This is the exception:</p>
  
  <pre><code>Request: http://localhost:8080/binaries/products/shortcode_1/test_image2.jpg raised org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
  </code></pre>
  
  <p>Anyone have any ideas? It's Spring 4.1.4.RELEASE</p>