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

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

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&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:

    {
        &quot;timeslots&quot;: [
            [
                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 = &quot;/timeslots/id/{id}&quot;)
    public void updateTimeslotsById(@RequestBody ArrayList&lt;Float&gt;[] timeslots, @PathVariable(&quot;id&quot;) String id) {
        Member member = this.memberService.getMemberById(id).orElseThrow(() -&gt;
                new ApiRequestException(&quot;Cannot find member with this ID&quot;));
        //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&lt;Float[]&gt; timeslots;
//getter
//setter
}

Change your method signature to

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:

确定