0

Is it possible to send a list of objects as a response to a HttpGet request in Java? I can't find anything about how to accomplish this.

user16655
  • 1,901
  • 6
  • 36
  • 60

1 Answers1

1

Yes, it possible to send the list of objects as a response to GET/POST response.

It would entirely depend on how you are exchanging data between client and server. For example, you are exchanging data in JSON then you have to use libraries like Jackson which can convert your Java object to JSON, if you are using XML then same told true for it.

Read here for available options with JSON, also feel free to read this answer, however it talks more about traversing JSON.


Updates based on OP's comment

Approach always remains same, you will send response to client in some format, you will use HttpServletResponse method like set content type etc. where you will set the content type (XML or JSON etc.) and write to the response stream and then flush the stream.

Please have a look at this question.

Community
  • 1
  • 1
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • I am trying to write the list to ServletOutputStream, but since I can't convert the list to bytes I'm stuck. Also, does the objects in the list have to be serializable? – user16655 Feb 03 '16 at 12:27
  • This makes your question different, read this now - http://stackoverflow.com/questions/10142409/write-an-inputstream-to-an-httpservletresponse – hagrawal7777 Feb 03 '16 at 12:31
  • Thank you for clearing it up :) – user16655 Feb 03 '16 at 12:55