简体   繁体   English

如何将 controller 中的 RequestBody 定义为列表,我收到 500 错误

[英]How to define RequestBody in controller as list, I am getting 500 error

I want to pass search input as list, Refer the below code and payload which i tried, I am getting parser errror.我想将搜索输入作为列表传递,请参考我尝试过的以下代码和有效负载,我收到解析器错误。

// code in searchinput // 搜索输入代码

public class SearchInput {
    private List<SearchCriteria> criterias;

}

// code in search criteria // 搜索条件中的代码

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

//code for controller //controller的代码

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

// payload which I tired // 我累了的有效载荷

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"
}

Have you tried this payload:您是否尝试过此有效负载:

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

The above payload which you are passing as request body represents array of SearchCriteria objects, so you can parse that json directly into List<SearchCriteria> and no need of SearchInput class您作为请求正文传递的上述有效负载表示SearchCriteria对象数组,因此您可以将 json 直接解析为List<SearchCriteria>而不需要SearchInput class

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果从数据库中获取空值,如何设置 NULL? 我收到 500 个内部服务器错误 - How to set NULL if getting empty value from database? I am getting 500 internal server error 如何在RecyclerView中实现更多负载? 我清单上有500件物品 - How to implement load more in a RecyclerView? I am getting 500 items in list 如何使用 HttpURLConnection for java android 发送身份验证密钥? 我收到错误代码 500 - How to send authentication key using HttpURLConnection for java android? I am getting error code 500 为什么在POST上收到500服务器错误? - Why am I getting a 500 server error on POST? 我在测试 controller 端点时收到错误 403 - I am getting error 403 when I test controller endpoint 为什么会收到http 500响应? - Why am I getting a http 500 response? 如何使requestbody控制器工作? - How to make requestbody controller works? 为什么在使用Ajax发送数据时在Struts 2中出现500(内部服务器错误)? - Why I am getting 500 (Internal Server Error) in Struts 2 while sending data using Ajax? 我使用 Spring 引导从后端收到状态 500 内部错误 - I am getting an Status 500 Internal Error from my backend with Spring Boot 为什么我收到“错误500:java.lang.NullPointerException” Java Servlet - Why am i getting a “Error 500: java.lang.NullPointerException” java servlets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM