I am creating a JSON REST service with Spring 3.0.5, and my answer contains the object from my request , although I have not added it. I am using MappingJacksonJsonView and Jackson 1.6.4 to render the ModelAndView object in JSON.
Custom object is simple
public class SimpleUser {
private String username;
private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password;
}
}
One of the queries looks like this:
@RequestMapping(value = "/register", method = RequestMethod.GET)
public ModelAndView register(SimpleUser user) {
ModelAndView mav = new ModelAndView();
mav.addObject("ok", "success");
return mav;
}
Then I call the service with
curl 'http://localhost:8080/register?username=mike&password=mike'
Expected response
{"ok": "success"}
The answer I get is
{"ok":"success","simpleUser":{"username":"mike","password":"mike"}}
Where and why is a custom object added to ModelAndView and how can I prevent this?
Possible Solution
One way around this is to use the Model instead of SimpleUser . This seems to work, but it should be possible to use a business object.
It works:
@RequestMapping(value = "/register", method = RequestMethod.GET)
public ModelAndView register(Model model) {
log.debug("register(%s,%s)", model.asMap().get("usernmae"), model.asMap().get("password"));
ModelAndView mav = new ModelAndView();
mav.addObject("ok", "success");
return mav;
}