I have a Spring MVC 3.1.0 project and I have configured a ContentNegotiatingViewResolver bean to automatically generate JSON output for a given endpoint (which uses org.springframework.web.servlet.view.json.MappingJacksonJsonView).
I have a few controller methods which add data to the JSP page (via model.addAttribute("foo", fooService.getFoo());) that I don’t want to appear in the JSON output.
I have tried adding a @JsonIgnore annotation to my service method getFoo() (which returns a Map<String, String>) but it doesn’t work. I still see the foo object being marshalled in my JSON output when I hit that controller.
Can anyone suggest another way of achieving this or tell me why the JsonIgnore annotation is not working?
MappingJacksonJsonViewserializes all the contents of the model into a json – all the objects that you have placed in your model object, so it does not matter if you have marked one of the service methods with@JsonIgnore, as long it ends up in the model which it does because of the call tomodel.addAttribute("foo"..it would get serialized. The fix could be simply to not add the model attribute, or to use@ResponseBodywhich will give you control over the specific response object that is being serialized.Another option is to specify the exact keys that you will be using when configuring
MappingJacksonJsonView: