Search code examples
javajsonspringspring-mvcspring-json

spring json writes more json then i want


I am using the json spring view in order to return a simple JSON object from my controller.

The problem is that its returning more data then i want. it is returning the validation errors and things inside of my model when all i am doing is this in the controller:

Map model = new HashMap()
model.put("success", "true");
return new ModelAndView("jsonView", model);

If you look at the bottom of this page in the docs it looks like i am getting back data that POST would return. i am not doing a post, i am doing a GET by going directly to the URL with params.

How do i get this lib to return just the data in my model?


Solution

  • We have used this library and it has worked as expected. Could you try with the following variation of the above code to see if it works? We use this in some places...

    ModelAndView modelAndView = new ModelAndView("jsonView");
    ModelMap map = modelAndView.getModelMap();
    map.addAttribute("success", "true");
    return modelAndView;