7

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)

....

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

Any ideas on that, guys?

2
  • possible it's getting default value {}? Commented Feb 7, 2013 at 11:34
  • Well, I found this solution in Google, but it doesn't work, actually. It doesn't even enter the controller method in the debug mode. When I don't use this default value, so the array is null and it throws an exception in logic. To check whether it is null is not a way, cause I have like 6 arrays passing as parameters. Commented Feb 7, 2013 at 11:36

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Have you found a solution?
@denu, The default empty array value is just " ", so you write value=""
Is that a string with space or an empty string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.