【发布时间】:2019-04-24 00:05:02
【问题描述】:
我有一个 REST 控制器,我在其中编写了这段代码
@PostMapping(value = "/otp")
public void otp(@RequestBody Integer mobile) {
System.out.println(" Mobile = "+mobile);
}
我使用以下输入从 Postman 调用此方法
URL : localhost:8080/otp
Body:
{
"mobile":123456
}
但我得到以下异常
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of java.lang.Integer out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token
如果我像这样将字符串作为参数
@PostMapping(value = "/otp")
public void otp(@RequestBody String mobile) {
System.out.println(" Mobile = "+mobile);
}
并将输入作为
{
"mobile":123456
}
现在在控制台打印如下
Mobile = {
"mobile":"123456"
}
但我只想要这个值123456。如何达到我的要求?
注意:我不想创建任何额外的 POJO 类,甚至不想使用查询/路径参数发送数据。
【问题讨论】:
-
我会说:而不是
{ "mobile" : 123456 }只需发送123456作为 HTTP 正文。或者,使用Integer字段创建一个类Mobile,并将其用作方法的参数。
标签: java json spring spring-boot spring-restcontroller