在Spring中发送数组列表作为请求主体时出现错误。

huangapple go评论99阅读模式
英文:

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在本地传递给我的应用程序时:

  1. {
  2. "timeslots": [
  3. [
  4. 8.4,
  5. 9.4,
  6. 10.0,
  7. 11.4,
  8. 12.0,
  9. 13.4,
  10. 14.4,
  11. 15.4,
  12. 16.4,
  13. 17.4,
  14. 18.4,
  15. 19.4
  16. ],
  17. [
  18. 10.4,
  19. 11.4,
  20. 12.0
  21. ]
  22. ]
  23. }
  24. ]

这是我的端点:

  1. @PutMapping(path = "/timeslots/id/{id}")
  2. public void updateTimeslotsById(@RequestBody ArrayList<Float>[] timeslots, @PathVariable("id") String id) {
  3. Member member = this.memberService.getMemberById(id).orElseThrow(() ->
  4. new ApiRequestException("无法找到此ID的成员"));
  5. //member.setTimeslots(timeslots);
  6. this.memberService.updateMember(id, member);
  7. }
英文:

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&lt;java.lang.Float&gt;` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList&lt;java.lang.Float&gt;` 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:

  1. {
  2. &quot;timeslots&quot;: [
  3. [
  4. 8.4,
  5. 9.4,
  6. 10.0,
  7. 11.4,
  8. 12.0,
  9. 13.4,
  10. 14.4,
  11. 15.4,
  12. 16.4,
  13. 17.4,
  14. 18.4,
  15. 19.4
  16. ],
  17. [
  18. 10.4,
  19. 11.4,
  20. 12.0
  21. ]
  22. ]
  23. }
  24. ]

This is my endpoint:

  1. @PutMapping(path = &quot;/timeslots/id/{id}&quot;)
  2. public void updateTimeslotsById(@RequestBody ArrayList&lt;Float&gt;[] timeslots, @PathVariable(&quot;id&quot;) String id) {
  3. Member member = this.memberService.getMemberById(id).orElseThrow(() -&gt;
  4. new ApiRequestException(&quot;Cannot find member with this ID&quot;));
  5. //member.setTimeslots(timeslots);
  6. this.memberService.updateMember(id, member);
  7. }

答案1

得分: 1

你的控制器方法中的接收器与 JSON 架构不符,因此会出现反序列化问题。

创建以下自定义类。

  1. class MyObject {
  2. List<Float[]> timeslots;
  3. // getter
  4. // setter
  5. }

将您的方法签名更改为

  1. 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.

  1. MyObject {
  2. List&lt;Float[]&gt; timeslots;
  3. //getter
  4. //setter
  5. }

Change your method signature to

  1. public void updateTimeslotsById(@RequestBody MyObject timeslots, @PathVariable(&quot;id&quot;) String id) {

huangapple
  • 本文由 发表于 2020年9月27日 08:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64083767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定