Reputation: 1044
I'm working on a project in wich I need to return a list of objects in json format. I'm using Spring-mvc framework with jackson library in order to do the serialization.
I want a json structure containing directly the objects, not a 'name:array of objects'.
This is my simplified code:
@RequestMapping(method = RequestMethod.GET, value = "/clients")
public List getClients(
@RequestParam(value = "estat", required = false) String estat
throws Exception {
List<Clients> l = s.mdClients(estat);
return l;
}
This "return l" that you see goes directly to Jackson, and jackson converts 'l' into an structure like:
{
"ClientsList": [
{
"x": "2",
"y": "5"
}
]}
The problem is the root "ClientsList". I want to get this ouput without root:
{
[
{
"x": "2",
"y": "5"
}
]}
So, anyone could help? thanks in advance!
Upvotes: 2
Views: 7631
Reputation: 1044
I've found the solution using @ResponseBody in my controller as @vacuum commented (thanks!):
@RequestMapping(method = RequestMethod.GET, value = "/clients")
public @ResponseBody List getClients(
@RequestParam(value = "estat", required = false) String estat
throws Exception {
List<Clients> l = s.mdClients(estat);
return l;
}
I also needed to change my output-conversion method, using
<mvc:annotation-driven />
in my servlet-context.xml, in order to use jackson library for the json conversion of my list.
The output now:
[
{
"x": "2",
"y": "5"
}
]
Upvotes: 2
Reputation: 2273
Try to add @ResponseBody
in method declaration:
public @ResponseBody List getClients()
Upvotes: 5