【问题标题】:Recieve list of objects in Spring Boot Controller在 Spring Boot Controller 中接收对象列表
【发布时间】:2021-10-29 15:23:47
【问题描述】:

我从 Spring Boot @RestController 中的 React 中制作的数据表插件接收到一些过滤器。该插件以这种方式发送过滤器: 过滤[0][id] = "tableColumnName" 过滤[0][值] = "tableColumnValue"

我收到了一些关于 DTO 的其他分页和相关信息,如下所示:

@GetMapping("/search")

public ResponseEntity<DashboardDTO> search(SearchDTO searchDTO){

}

我的 SearchDTO 是这样的:

public class SearchDTO{
    public int pageNumber;
    public int pageSize;
    public int total;
    public int pages;
    @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
    public List<Long> ids;
    public List<FilterDTO> filters;
}
    

如果我尝试添加,例如一个FilterDTO的List,其中FilterDTO如下:

public class SearchFilters {
    public int id;
    public String value;
}

然后我收到以下错误:

property referenced in indexed property path is neither an array nor a list nor a map

请求如下:

endpoint/search?pageSize=5&pageNumber=0&filtered[0][id]=columnName1&filtered[0][value]=value1&filtered[1][id]=columnName2&filtered[1][value]=value2
   

如何在控制器中接收此类参数?

【问题讨论】:

  • 你能添加一个带有过滤参数的示例网址吗?
  • 您能否添加FilterDTO 的代码以及对应于“如果我尝试添加,例如,FilterDTO (...) 的列表”的代码?谢谢!
  • 感谢您的 cmets,刚刚使用 FilterDTO 和示例 url 编辑了帖子。我认为问题在于解析器无法将 id 识别为“filtered[0][id]”中的属性,但如果它被过滤[0].id 它将起作用。

标签: java spring-boot filter query-parameters


【解决方案1】:

经过一些研究,我无法从查询字符串中获取它们作为参数,但我可以从正文中获取它们。问题是前端使用的序列化器是 filtered[0][id] 而不是 filtered[0].id,所以后端解析器不知道 id 是一个属性。

虽然我可以通过 @RequestBody 将它作为一个主体来获取,但它是一个 POST 请求,所以它不是来自查询字符串。

【讨论】:

    猜你喜欢
    • 2019-05-10
    • 1970-01-01
    • 2019-05-10
    • 2018-12-21
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多