mardi 21 février 2017

How to pass array of JSON objects with Files from Spring Controller?

I want to have a Spring REST Controller that returns an array of objects containing a file, basically a picture. What I could do is:

  1. Create a POJO
  2. Get the image and convert to a base 64 string
  3. Put this string as a field in the POJO
  4. Add this POJO to an ArrayList
  5. Repeat steps 1-4 as necessary
  6. Attach the ArrayList to the ResponseEntity
  7. Send back the response entity to the client
  8. From the client, get the file and paint the image in a canvas

or in code, something like this:

@RequestMapping(value = "/getInfo")
public ResponseEntity<Object> getInfo() throws Throwable {

    ArrayList<Object> objList = new ArrayList<Object>();

    for (int i = 0; i < 10; i++) {
        Object obj = new Object();

        obj.setField1(0);
        obj.setField2(0);

        String stringImg = // Convert image to base 64 string

        obj.setStringImg(stringImg);
        objList.add(obj);
    }

    return new ResponseEntity<Object>(objList, HttpStatus.OK);
}

I've heard that converting and using base 64 string wastes space and is not really best practice. Is there an alternative or better way of doing this?




Aucun commentaire:

Enregistrer un commentaire