-2

I need to find an optimised way to pass an arraylist as json a obj to the controller using spring mvc.

Currently I am doing it this way:

public @ResponseBody List<String> abcd() throws Exception {
    return listString;
}

but it is returning a normal list type object. I need a proper json obj which can be accessed by angular js.

Joe
  • 4,877
  • 5
  • 30
  • 51
BitastaB
  • 5
  • 1
  • 4
  • There are a ton of Java libraries for conversion from and to JSON. Please research. Also, do not use raw types (`List` instead of `List`.) – RealSkeptic Jan 18 '17 at 13:14

2 Answers2

1

@ResponseBody should return a JSON object but here is what you can try:

public @ResponseBody ArrayList<YourObject>
    myFunction(HttpServletResponse response) {
    response.setContentType("application/json");     
    return yourArrayList;`
}

You can also try returning a Map instead. Keep your return type as Map <String, Object> and put your ArrayList in the map. This will also work.

mic4ael
  • 7,974
  • 3
  • 29
  • 42
Kaushik
  • 34
  • 4
0

Try to put your list inside an object like

class JSONResponse{
 List<String> names;
 //other nodes of JSON
}

Return the object as response You can also replace String with the objects you desire. Please post the code if you need clear help.

Saket Puranik
  • 115
  • 2
  • 8