更新时间:2022-06-11 17:23:14
显然杰克逊无法反序列化将JSON传递给 Integer
。如果您坚持通过请求正文发送用户的JSON表示,则应将 userId
封装在另一个bean中,如下所示:
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
}
然后使用该bean作为处理程序方法参数:
Then use that bean as your handler method argument:
@RequestMapping(...)
public @ResponseBody Record getRecord(@RequestBody User user) { ... }
如果你不喜欢创建另一个bean的开销,你可以通过 userId
作为路径变量的一部分,例如 /的getUser / 15
。为了做到这一点:
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) { ... }
由于您不再在请求正文中发送JSON,因此应删除消耗
属性。
Since you no longer send a JSON in the request body, you should remove that consumes
attribute.