title: Spring boot return application/pdf with a ResponseEntity<Resource>
body: <p>In my springboot application, I try to return a pdf file with a ResponseEntity-Resource- to mock a service. So I can't change the return type of this method.</p>
  
  <p>My code :</p>
  
  <pre><code>@RequestMapping(
          value = "/pdf",
          produces = MediaType.APPLICATION_PDF_VALUE,
          method = RequestMethod.GET
  )
  public ResponseEntity&lt;Resource&gt; getpdf() {
      try {
          ClassPathResource pdfFile = new ClassPathResource("sample.pdf");
  
          HttpHeaders headers = new HttpHeaders();
          headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
          headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT");
          headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "Content-Type");
          headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
          headers.add(HttpHeaders.PRAGMA, "no-cache");
          headers.add(HttpHeaders.EXPIRES, "0");
          headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + pdfFile.getFilename());
          headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PDF_VALUE);
  
          log.info("pdfFile.contentLength() : " + pdfFile.contentLength());
  
          return ResponseEntity
                  .ok()
                  .headers(headers)
                  //.contentLength(pdfFile.contentLength())
                  //.contentType(MediaType.APPLICATION_PDF)
                  .body(new InputStreamResource(pdfFile.getInputStream()));
      } catch (IOException e) {
          log.error("Couldn't serialize response for content type ", e);
          return new ResponseEntity&lt;&gt;(HttpStatus.INTERNAL_SERVER_ERROR);
      }
  }
  </code></pre>
  
  <p>When I try to call this get with postman or swagger I have a 406 with response header :</p>
  
  <pre><code>{   
  "date": "Fri, 02 Nov 2018 14:04:44 GMT",   
  "content-length": "0", 
  "content-type": null 
  }
  </code></pre>
  
  <p>Does anyone have an idea?</p>
  
  <p><a href="https://i.stack.imgur.com/7xqvA.png" rel="nofollow noreferrer">Swagger response</a></p>