Spring mvc空数组作为默认值 [英] Spring mvc empty array as a default value

查看:270
本文介绍了Spring mvc空数组作为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a spring controller method that receives some optional arrays as parameters. They are not required, but I'd like them to be not null, but just empty arrays when they are are not in the parameters which are received by the controller method. I know, I could check them for being null and then assign as new object, but that would generate lots of boilerplate code. Also, when I try to do something like this:

@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中提供参数时,这很奇怪,因为必填字段为false。

I receive the 400th error, when I don't provide the parameter in the url, which is weird, cause the required field is false.

任何想法,伙计们?

推荐答案

找到解决方案。默认的空数组值只是。但问题是即使参数不是必需的,它也会抛出第400个错误。奇怪的。正在努力。

Found the solution. The default empty array value is just "". But the problem is that even though the parameter is not required, it throws the 400th error. Weird. Working on that.

这篇关于Spring mvc空数组作为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆