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:
- Create a POJO
- Get the image and convert to a base 64 string
- Put this string as a field in the POJO
- Add this POJO to an ArrayList
- Repeat steps 1-4 as necessary
- Attach the ArrayList to the ResponseEntity
- Send back the response entity to the client
- 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