0

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
Community
  • 1
  • 1
a3.14_Infinity
  • 5,653
  • 7
  • 42
  • 66
  • "Can we create a POJO object containing byte array (image) and other ..." Yes you can – Jens Jun 13 '16 at 12:01
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – Roman C Jun 13 '16 at 13:01

2 Answers2

1

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

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • You can create a map on the fly to hold the image data and the information, without creating the POJO – Joram Jun 13 '16 at 14:18
  • @Joram that's still just wrapping the data inside of an Object. You either want to use an Object (1st option) or you don't (2nd option). Using a custom POJO is preferred instead of a Map implementation, because it is usually better to work with in the future. – Dropout Jun 13 '16 at 14:25
0

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.

Deepanjan
  • 649
  • 2
  • 8
  • 15