gpt4 book ai didi

java - Spring mvc 空数组作为默认值

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:56:01 25 4
gpt4 key购买 nike

我有一个 spring Controller 方法,它接收一些可选数组作为参数。它们不是必需的,但我希望它们不为空,而只是空数组,当它们不在 Controller 方法接收的参数中时。我知道,我可以检查它们是否为空,然后分配为新对象,但这会生成大量样板代码。另外,当我尝试做这样的事情时:

@RequestMapping(headers = "Accept=application/json", method = RequestMethod.GET, value = "/socialUsers/saveFilter", produces = "application/json")
public @ResponseBody
void saveFilterToDataBase(@RequestParam(required = false, value = "gender", defaultValue = "{}") Gender[] genders)

....

我收到第 400 个错误,当我没有在 url 中提供参数时,这很奇怪,因为必填字段是错误的。

伙计们,对此有什么想法吗?

最佳答案

找到解决方案。默认的空数组值只是“”。但问题是,即使参数不是必需的,它也会抛出第 400 个错误。诡异的。致力于此。

关于java - Spring mvc 空数组作为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14749691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com