JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token
Obviously Jackson can not deserialize the passed JSON into an Integer
. If you insist to send a JSON representation of a User through the request body, you should encapsulate the userId
in another bean like the following:
public class User {
private Integer userId;
// getters and setters
}
Then use that bean as your handler method argument:
@RequestMapping(...)
public @ResponseBody Record getRecord(@RequestBody User user) { ... }
If you don't like the overhead of creating another bean, you could pass the userId
as part of Path Variable, e.g. /getuser/15
. In order to do that:
@RequestMapping(value = "/getuser/{userId}", method = POST, produces = "application/json")
public @ResponseBody Record getRecord(@PathVariable Integer userId) { ... }
Since you no longer send a JSON in the request body, you should remove that consumes
attribute.
Perhaps you are trying to send a request with JSON text in its body from a Postman client or something similar like this:
{
"userId": 3
}
This cannot be deserialized by Jackson since this is not an Integer (it seems to be, but it isn't). An Integer object from java.lang Integer is a little more complex.
For your Postman request to work, simply put (without curly braces { }):
3