0

I try to return data from controller to AJAX success() callback. This code perfectly works for 100,000 objects. But when I increase size of list to 1 million objects, it goes to success() callback, but returning null as response. Could you help me with finding the way to return 1 mln of objects?

 A.$.ajax({
            url: 'getData',
            dataType: 'json',
            success: function (response) { 
            alert(response)
            }

@RequestMapping("/getData")
public  @ResponseBody
 void download(HttpServletResponse response ) throws IOException, JSONException
 {
  List <Objects> list = getListFromService();
  response.setContentType("application/json"); 
  PrintWriter out = response.getWriter();  
  ObjectMapper mapper = new ObjectMapper();
  String jsonString = mapper.writeValueAsString(list);
  out.print(jsonString);
  out.flush(); 
 }
WomenWhoCode
  • 396
  • 1
  • 6
  • 18
  • 1
    As an aside, why would you want to return a million objects at once? How would you present that much data to the user? – nnnnnn Sep 04 '17 at 04:03
  • @nnnnnn I would like to put them all into csv file for user to download. I know that I can do at server side in controller as well. Just wanted try both approaches and figure out what is the best way. But I think, I'll stay with server-side file generation – WomenWhoCode Sep 04 '17 at 04:07
  • perhaps your server is limiting the response size - check the browser **developer** tools console and network tab for any indication of this – Jaromanda X Sep 04 '17 at 04:10

2 Answers2

0

Your method is annotated with @ResponseBody but it's return type is void. Try to return your List directly, and let Spring handle the JSON processing for you:

@RequestMapping("/getData")
public @ResponseBody List<Objects> download( ) {
    return getListFromService();
}
0

There is a limit set by server and the spring too probably. Check this link.

In such case you should mostly see error in the logs for the limit being exceeded.