代码之家  ›  专栏  ›  技术社区  ›  selvi

如何将控制器中的RequestBody定义为list,我得到了500个错误

  •  2
  • selvi  · 技术社区  · 4 年前

    我想通过搜索输入列表,参考下面的代码和有效载荷,我尝试,我得到解析器错误。

    public class SearchInput {
        private List<SearchCriteria> criterias;
    
    }
    

    //搜索条件中的代码

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

    //控制器代码

    @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"
    }
    
    2 回复  |  直到 4 年前
        1
  •  2
  •   Donato Amasa    4 年前

    {
        "criterias" : [
            {
                "key": "g",
                "operation": "eq",
                "value": "m"
            },
            {
                "key": "name",
                "operation": "like",
                "value": "Rani"
            },
            {
                "key": "sh",
                "operation": "eq",
                "value": "Never"
            }
        ]
    }
    
        2
  •  1
  •   Ryuzaki L    4 年前

    SearchCriteria 对象,因此您可以将该json直接解析为 List<SearchCriteria> 而且不需要 SearchInput

    @PostMapping("/searchhh")
    public List<Profile> findProfiles(@RequestBody List<SearchCriteria> input) {
                System.out.println("criterias=" + input);
        
        return null;
    }
    
        3
  •  0
  •   David.Lee    4 年前

    你的类有get/set方法吗?