Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC Controller adds request object to response
    primarykey
    data
    text
    <p>I am building a JSON REST service with Spring 3.0.5 and my <strong>response</strong> contains the object from my <strong>request</strong> although I did not add it. I am using the <em>MappingJacksonJsonView</em> and Jackson 1.6.4 for rendering the ModelAndView object to JSON.</p> <p>The User object is simple</p> <pre><code>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; } } </code></pre> <p>One of the requests looks like this</p> <pre><code>@RequestMapping(value = "/register", method = RequestMethod.GET) public ModelAndView register(SimpleUser user) { ModelAndView mav = new ModelAndView(); mav.addObject("ok", "success"); return mav; } </code></pre> <p>Then I call the service with</p> <pre><code>curl 'http://localhost:8080/register?username=mike&amp;password=mike' </code></pre> <p>The response I expect is </p> <pre><code>{"ok": "success"} </code></pre> <p>The response I get is</p> <pre><code>{"ok":"success","simpleUser":{"username":"mike","password":"mike"}} </code></pre> <p>Where and why is the user object added to the ModelAndView and how can I prevent that?</p> <p><strong>Possible solution</strong></p> <p>One way to work around this is to use <strong>Model</strong> instead of <strong>SimpleUser</strong>. This seems to work but it should be possible to use the business object.</p> <p>This works:</p> <pre><code>@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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload