-1

I'm using spring-mvc to create a @RestController webservice.

Question: how can I expose a ResultSet from a sql query via the webservice, as JSON or XML.

The column names should represent the json/xml field name.

xml: <col1>myval1<col1>

json: "col1": myval1

Is that simply possible using some apache or spring utility?

Important note: I do not want to map the sql result to a pojo bean with getters and setters before. I just want to expose the results without converting to a predefined bean before.

Like php supports it using json_encode(rows).


Update: I cannot post a solution as the question is locked, but I found a neat way achieving the result with spring:

List<Map<String, Object>> resultSet = new JdbcTemplate(dataSource).queryForList(..);

You then can just return this List as response via a @RequestMapping method inside @RestController. Spring will take care of the rest and marshal it as requested, eg as JSON or XML.

No need to write custom converters to json objects.

membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

0

You can refer this for your solution . Hope this will help you.

Nishant Bhardwaz
  • 924
  • 8
  • 21
  • So there is no buildin or framework solution? Like php supports it out of the box – membersound Jun 21 '17 at 12:30
  • Please read [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) before attempting to answer more questions. –  Jun 21 '17 at 15:26