【问题标题】:How to send List value in Request Body to Rest Api如何将请求正文中的列表值发送到 Rest Api
【发布时间】:2023-03-05 13:27:02
【问题描述】:

您好,我已经编写了一个 Rest Service 来接受 Long 值列表作为通过 RequestBody 的输入,下面给出了相同的代码:

@DeleteMapping("/files")
public ResponseEntity<?> deletefiles(@RequestBody List<Long> ids) {
     fileService.deleteSelectedfiles(ids);
     return ResponseEntity.ok().build();
}

当我尝试从 Postman 访问上述网址时,我收到以下错误:

"JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: [![enter image description here][1]][1]Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token\n at [Source: (PushbackInputStream); line: 1, column: 1]"

在 Postman 中,我以以下格式将数据作为原始数据发送

{"ids": [1 ,2]} 

谁能帮我解决这个问题

【问题讨论】:

  • 您的请求正文不是数字数组。
  • 粘贴你的js代码函数

标签: java json rest spring-boot postman


【解决方案1】:
@RequestMapping(YOUR_REQUEST_MAPPINGS)
public void testArrayOfValues(@RequestParam List<String> values) 
{
  
}

【讨论】:

    【解决方案2】:

    你的有效载荷应该是一个

    [1 ,2]
    

    代替

    {"ids": [1 ,2]}
    

    第一个选项是一个 json 数组,第二个例子是一个 json body。 您可以将第一个与@RequestBody List&lt;Long&gt; ids 一起使用,或者将第二个与@RequestBody YourData data 一起使用

    class YourData {
        List<Long> ids
    }
    

    【讨论】:

    • 我已经尝试过您的建议@star67,但我收到以下异常:org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文:public org.springframework.http.ResponseEntity>
    猜你喜欢
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 2017-03-14
    • 2016-11-08
    相关资源
    最近更新 更多