title: Spring Rest Service that returns different media type based on logic
body: <p>HELP! Ive tried everything I can and nothing seems to work. My issue is as follows:</p>
  
  <p>I've got a Spring REST service, that makes a REST call, receives a base64 encoded PDF. I then decode the PDF, and display it in the browser. I can get this part to work perfectly! BUT! I am having issues handling 404s, 500s, ect. ERROR messages.  </p>
  
  <p>Unfortunately, Error messages are served back to me in the API as a message in a hashmap, as you can see by my logic below.</p>
  
  <p>Ive tried creating PDFs, but I cant convert that into a File Object, I've tried to Stream the bytes directly to the browser, but it isnt working for me, no matter what I do.</p>
  
  <p>Is there a way to change the Response Headers based on Logic? When successful, return a pdf, and when error return a text file to the browser?</p>
  
  <p>Im completely exhausted here and am not sure where to go. Any advice or pointers would be extremely appreciated.</p>
  
  <p>Ive also tried throwing a 404 to the browser, but its expecting a pdf file, and I get MIME errors.</p>
  
  <pre><code>@Controller
  @Path("/mon")
  public class WSController {
  
      @Autowired
      Service service;
  
      @GET
      @Produces("application/pdf")
      @Path("/ws")
      public File getWs(@QueryParam("vin") String vin) throws IOException, URISyntaxException {
          Map response = service.getWs(vin);
          if (response == null) {
              return createErrorFile("Server Error, please try again later.");
          } else if (response.get("ErrorCode") != null &amp;&amp; response.get("ErrorCode").equals("E")) {
              return createErrorFile((String) response.get("ErrorDescription"));
          } else {
              return decodeBase64(((String) response.get("Base64String")).replace("\n",""));
          }
      }
  
      private File decodeBase64(String encodedFile) throws IOException {
          byte[] decodedBytes = DatatypeConverter.parseBase64Binary(encodedFile);
  
          File file = new File("wS.pdf");
          FileOutputStream fop = new FileOutputStream(file);
  
          fop.write(decodedBytes);
          fop.flush();
          fop.close();
  
          return file;
      }
  
      private File createErrorFile(String errorDescription) throws IOException {
          //Not sure what to do here
          return null;
      }
  </code></pre>
  
  <p>EDIT: Here is what ended up working for me. Thank you so much for the advice. This has been a wonderful learning experience.</p>
  
  <pre><code>@GET
  @Path("/ws")
  public Response getWS(@QueryParam("vin") String vin) throws IOException, URISyntaxException {
      Map results = Service.getWS(vin);
      if (results == null) {
          Response.ResponseBuilder rBuild = Response.status(Response.Status.BAD_REQUEST);
          return rBuild.type(MediaType.TEXT_PLAIN)
                  .entity("Server error, please try again later.")
                  .build();
      } else if (results.get("ErrorCode") != null &amp;&amp; results.get("ErrorCode").equals("E")) {
          Response.ResponseBuilder rBuild = Response.status(Response.Status.BAD_REQUEST);
          return rBuild.type(MediaType.TEXT_PLAIN)
                  .entity(results.get("ErrorDescription"))
                  .build();
      } else {
          File responseData = decodeBase64(((String) results.get("Base64String")).replace("\n", ""));
          Response.ResponseBuilder rBuild = Response.ok(responseData, "application/pdf");
          return rBuild.build();
      }
  }
  </code></pre>