2

I'm POSTing a JSON request to a Spring 3.0 controller. The method signature is...

@RequestMapping(value="/add", method=RequestMethod.POST)
public @ResponseBody Map<String, ? extends Object> add(@RequestBody Entry)

The JSON looks like this...

{"user":"1"}

The Entry object has one attribute of type User.

When a request is submitted this error is thrown,

org.codehaus.jackson.map.JsonMappingException: Can not construct instance of com.x.y.z.Entry, problem: no suitable creator method found

I'm guessing the error is due to the fact that user on Entry is of type User rather than String ("1" is being passed in on the JSON).

Is there a way of taking the "1" coming in and using it to create a real User object (by looking it up in the database in this case)?

0

1 Answer 1

2

Does Entry have a parameterless constructor?

That's your first place to look. Normally errors of this nature occur because the code is looking for a parameterless constructor to create Entry with.

Your idea, that is to create User as the real user is fine, but it should be done after this method is called, in some other layer or something. You wanna keep things simple by not interfering with the marshalling of the json. You can add onto this with another layer.

Sign up to request clarification or add additional context in comments.

Comments

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.