Currently, I need to send a large json object to an ajax request. For the purpose I am using the following controller method which works fine.
@RequestMapping(method = RequestMethod.POST,params = {"dynamicScenario"})
@ResponseBody
public String getDynamicScenarioData(@RequestParam Map<String, String> map) throws JsonParseException, JsonMappingException, IOException
{
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String,Object> queryParameters = mapper.readValue(map.get("parameters") , Map.class);
Map<String, Object> getData = service.runDynamicScenario(queryParameters, map.get("queryString"));
return writer.writeValueAsString(getData); //here java throws java.lang.OutOfMemoryError: Java heap space memory
}
Update: my ajax is:
$.ajax({
type: "POST",
url: "dynamicScenario.htm",
data : tags,
dataType: "json",
success: function(data){});
My DispatcherServlet settings:
public class ApplicationInitializer implements WebApplicationInitializer
{
public void onStartup(ServletContext servletContext) throws
ServletException
{
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(ApplicationConfig.class);
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic servletRegistration = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
servletRegistration.setLoadOnStartup(1);
servletRegistration.addMapping("*.htmlx");
}
}
I am using jackson to serialize a map of different objects and then send it back to the ajax. However, if the size of the json is large then java throws out of memory. I know the Jackson method writer.writeValueAsString is inefficient because its writing to a string but is there any other alternative? I can't use plain java POJO because I don't know what objects the map which I will have to serialize will contain so I can't simply map it to some java object. Any ideas? Thanks