java - 如何以 JSON 格式发送整数值并在 REST 控制器中接收?
问题描述
我有一个 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
如果我将 String 作为这样的参数
@PostMapping(value = "/otp")
public void otp(@RequestBody String mobile) {
System.out.println(" Mobile = "+mobile);
}
并将输入传递为
{
"mobile":123456
}
现在它在控制台中打印如下
Mobile = {
"mobile":"123456"
}
但我只想要这个值123456
。如何达到我的要求?
注意:我不想创建任何额外的 POJO 类,甚至不想使用查询/路径参数发送数据。
解决方案
如果你想像这样发送你的身体:
{
"mobile":123456
}
您将创建另一个对象来接收该值。
但是,如果您只想接受整数值而不接受任何其他对象,则不会将 json 对象放入请求正文中,而只会放入整数本身。
Body:
12345
推荐阅读
- javascript - 是否有任何特定方法可以停止执行请求?
- sql - 查找分区中的第一个非空值
- android - 可以从单个设备一遍又一遍地订阅 FCM 主题吗?
- docusignapi - 无法添加新用户来创建新组织
- python - 在 OpenCV 中增加某些图像区域的强度
- javascript - 3D Globe.gl 的性能问题
- java - 使用 Spring Boot Actuator 和 git-commit-id-maven-plugin 格式化 Git/构建时间信息
- javascript - NodeJS中的Jinja SQL等价物?
- python-3.x - CSV 到 XML 交替值
- haskell - Rust 向量中的 Haskell 样式的链表添加