简体   繁体   中英

Is it possible to retrieve Image and JsonObject in a single Rest Service Call?

Using How to send an Image from Web Service in Spring , I am able to send Image as a response to rest service call. But, I would like to send back Image and some information using json object to client using rest web service.

How do I do it?

Can we create a POJO object containing byte array (image) and other fields (other information) and send back to client like:

  • Get Information and Image in single call

OR we always need to stick to separate calls for images like:

  • Get Information
  • Get Image

You've answered your question. Either use a POJO or split the calls. Java can't return tuples. What is better depends on circumstances:

  • if you don't want to make two calls and you're OK with handling the received object go for POJO

  • if you want to avoid using objects/envelopes for communication or if you want to use the calls separately go for two calls

Yes you can send as well as retrieve the image within the JSON. Try to save the image as a Base64 String using @Lob annotation in spring and convert the image file to base64 in javascript using FileReader object and image will be rendered as a base64 string along with the JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM