java - 如何在 Controller 中将 RequestBody 定义为列表,我收到 500 错误

标签 java spring-boot

我想将搜索输入作为列表传递,请参阅我尝试过的以下代码和有效负载,我遇到解析器错误。

//搜索输入中的代码

public class SearchInput {
    private List<SearchCriteria> criterias;

}

//搜索条件中的代码

public class SearchCriteria {
    private String key;
    private String operation;
    private String value;
}

// Controller 代码

@PostMapping("/searchhh")
    public List<Profile> findProfiles(@RequestBody SearchInput input) {
        List<SearchCriteria> criterias = input.getCriterias();
                System.out.println("criterias=" + criterias);
        
        return null;
    }

//我厌倦的有效负载

URL:
http://localhost:5555/matrimony/api/v1/profiles/searchhh

Request body:
[
  {
    "key": "g",
    "operation": "eq",
    "value": "m"
  },
  {
    "key": "name",
    "operation": "like",
    "value": "Rani"
  },
  {
    "key": "sh",
    "operation": "eq",
    "value": "Never"
  }
]

Response:
{
    "message": "JSON parse error: Cannot deserialize instance of `com.model.SearchInput` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.model.SearchInput` out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]",
    "status": 500,
    "timestamp": "2021-01-21T11:31:48.228796"
}

最佳答案

您是否尝试过此有效负载:

{
    "criterias" : [
        {
            "key": "g",
            "operation": "eq",
            "value": "m"
        },
        {
            "key": "name",
            "operation": "like",
            "value": "Rani"
        },
        {
            "key": "sh",
            "operation": "eq",
            "value": "Never"
        }
    ]
}

关于java - 如何在 Controller 中将 RequestBody 定义为列表,我收到 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65822124/

相关文章:

java - 与声音关联的应用程序飞出模拟器

reactjs - 我应该将用户数据存储到数据库或Redis缓存的哪里

spring-boot - 未调用@GetMapping 方法

reactjs - 如何从 Spring Boot 应用程序提供 NextJs 应用程序?

java - 模拟 FeignClient 响应

java - 我收到错误 java.lang.ClassCastException :

java - 格式化 .txt 文件中的数据的正确方法是什么? ( java )

c# - 向流畅的界面构建器添加 fork

java - 设备旋转后,提示文本将变为 EditText 中的真实文本

spring - 迁移到 Spring Boot 2.2.0 @JsonIgnore 不起作用