3

I am building a JSON REST service with Spring 3.0.5 and my response contains the object from my request although I did not add it. I am using the MappingJacksonJsonView and Jackson 1.6.4 for rendering the ModelAndView object to JSON.

The User 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 requests 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'

The response I expect is

{"ok": "success"}

The response I get is

{"ok":"success","simpleUser":{"username":"mike","password":"mike"}}

Where and why is the user object added to the ModelAndView and how can I prevent that?

Possible solution

One way to work around this is to use Model instead of SimpleUser. This seems to work but it should be possible to use the business object.

This 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;
}
0

2 Answers 2

3

It looks like you're trying to process a form submission and retrieve the result via ajax. If this is the case, you don't want to return a ModelAndView object. Use the @ResponseBody annotation to have Jackson represent your return object as a json object.

public @ResponseBody Map registerUser(SimpleUser user){
     Map responseMap = new HashMap();
     if(registerUser(user)){
          responseMap.put("OK", "Success");
     } else {
          responseMap.put("OK", "Failure");
     }
     return responseMap;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hello @EndlessLoop i also face the same problem, but i am using ContentNegotiationViewResolver with XStreamMarshller. Click on the following link for see my problem: stackoverflow.com/questions/25037473/…
1

For Spring 3.1.x You can set the modelKey property in org.springframework.web.servlet.view.json.MappingJacksonJsonView in your *servlet.xml like below:

Servlet.xml:

<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
       <property name="modelKey" value="appResponse"/>
</bean>

Request Method:

@RequestMapping(value="/access") 
public @ResponseBody Model getAccess(Model model) {

  ...
  model.addAttribute("appResponse", responseDetails);
  ...

  return model;
}

When you set a specific modelKey, all other attributes attached the the model will be ignored, hence the form parameters/request parameters. In additional, this provides a clearer design if your are presenting views for multiple media types (application/xml or application/json).

1 Comment

Hello @MasterV i also face the same problem, but i am using ContentNegotiationViewResolver with XStreamMarshller. Click on the following link for see my problem: stackoverflow.com/questions/25037473/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.