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.