英文:
Error When Sending Array of Lists As A Request Body In Spring
问题
我在Spring控制台中收到以下错误:
WARN 1208 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON解析错误: 无法从START_OBJECT令牌反序列化`java.util.ArrayList<java.lang.Float>`实例; 嵌套异常是com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法从START_OBJECT令牌反序列化`java.util.ArrayList<java.lang.Float>`实例 在[来源: (PushbackInputStream); 行: 2, 列: 5] (通过引用链: java.lang.Object[][0])]
当我将此响应正文通过Postman Echo在本地传递给我的应用程序时:
{
"timeslots": [
[
8.4,
9.4,
10.0,
11.4,
12.0,
13.4,
14.4,
15.4,
16.4,
17.4,
18.4,
19.4
],
[
10.4,
11.4,
12.0
]
]
}
]
这是我的端点:
@PutMapping(path = "/timeslots/id/{id}")
public void updateTimeslotsById(@RequestBody ArrayList<Float>[] timeslots, @PathVariable("id") String id) {
Member member = this.memberService.getMemberById(id).orElseThrow(() ->
new ApiRequestException("无法找到此ID的成员"));
//member.setTimeslots(timeslots);
this.memberService.updateMember(id, member);
}
英文:
I get the following error in the Spring console:
WARN 1208 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList<java.lang.Float>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<java.lang.Float>` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 2, column: 5] (through reference chain: java.lang.Object[][0])]
When I pass this response body to my application locally using Postman Echo:
{
"timeslots": [
[
8.4,
9.4,
10.0,
11.4,
12.0,
13.4,
14.4,
15.4,
16.4,
17.4,
18.4,
19.4
],
[
10.4,
11.4,
12.0
]
]
}
]
This is my endpoint:
@PutMapping(path = "/timeslots/id/{id}")
public void updateTimeslotsById(@RequestBody ArrayList<Float>[] timeslots, @PathVariable("id") String id) {
Member member = this.memberService.getMemberById(id).orElseThrow(() ->
new ApiRequestException("Cannot find member with this ID"));
//member.setTimeslots(timeslots);
this.memberService.updateMember(id, member);
}
答案1
得分: 1
你的控制器方法中的接收器与 JSON 架构不符,因此会出现反序列化问题。
创建以下自定义类。
class MyObject {
List<Float[]> timeslots;
// getter
// setter
}
将您的方法签名更改为
public void updateTimeslotsById(@RequestBody MyObject timeslots, @PathVariable("id") String id) {
英文:
Receiver in your contoller method does not comply with json schema hence deserialization issue is coming.
Craete below custom class.
MyObject {
List<Float[]> timeslots;
//getter
//setter
}
Change your method signature to
public void updateTimeslotsById(@RequestBody MyObject timeslots, @PathVariable("id") String id) {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论