javaarraysspring-bootspring-hateoashateoas

Springboot HATEOAS for PDF, Image and Zip Files


This is my first time working with HATEOAS everyone and I have come to a screeching halt. Here is my problem, I have an app that converts text from a text area to a pdf and a pdf to images. My issue is that I send both back as a ResponseEntity<byte[]>. Here is a sample success response from the text to pdf endpoint:

    // Get PDF from conversion result
    byte[] res;
    res = this.textToPDFService.convertTextToPdf(input);
    // Create pdf name
    DateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy:hh:mm:ss");
    String currentDateTime = dateFormatter.format(new Date());
    // Prepare Headers to open PDF on the client
    String headerKey = "Content-Disposition";
    String headerValue = "inline; filename=pdf_" + currentDateTime + ".pdf";
    // Send a successful response
    return ResponseEntity.ok()
            .header(headerKey, headerValue)
            .contentType(MediaType.APPLICATION_PDF)
            .body(res);

Questions:

  1. How can I send these back in the HATEOAS required JSON format and actually receive a PDF and a zip file? I heard something about Base64 encoding and decoding to handle it as a String, but I don't quite understand how it creates the files somehow and if it does.
  2. Don't I need the request headers for application/pdf and application/zip?

Any additional good sources on how I can add these files to a JSON response are very welcome.


Solution

  • Server-side: Send any of the above as a byte[] in a response entity, which Springboot automatically transformed using Base64 encoding to a String. This String is part of a JSON response sent by the server.

    Client-side: I receive the response. Then I create a LinkedHashMap<String, String> in which I store the media String using the key I set on the server. Then, decode it the value using the built-in Base 64 decoder (no packages/external repositories).