-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request parameters cannot bind to Optional<List<String>> [SPR-13418] #17997
Comments
Kazuki Shimizu commented I've submit PR of repro project. |
Rossen Stoyanchev commented It looks like when a request parameter has 2 values the As a workaround in this case you can remove Optional. The use of defaultValue implies its optional and it will always have a value. |
Rossen Stoyanchev commented Juergen Hoeller, it seems that ObjectToOptionalConverter ought to be registered earlier, perhaps ahead of the collection converters? |
Juergen Hoeller commented Rossen Stoyanchev, indeed, worth a try... Juergen |
Kazuki Shimizu commented Thanks for comments!!
I know the workaround and tried it. e.g.) @RequestMapping(path="search")
public String search(@RequestParam String type, @RequestParam Optional<List<String>> words, Model model) {
model.addAttribute("words", String.valueOf(words.orElseGet(()->defaultValue(type))));
// ...
}
private List<String> defaultValue(String type){
// it is dummy code.
return "1".equals(type) ?
Arrays.asList("word1", "word2", "word3") :
Arrays.asList("word4", "word5", "word6");
} |
Juergen Hoeller commented Let's do this in a broader fashion for 4.3, making sure For the time being, when not using Juergen |
Tor Arne Kvaløy commented As of version 4.3.2 I still only get the first element in the list. @RequestParam(required = false) Optional<List<String>> locations |
Kazuki Shimizu opened SPR-13418 and commented
Request parameter cannot bind to
Optional<List<String>>
at the@RequestMapping
method.I define the following method as handler method(
@RequestMapping
method).I submit a request to
http://localhost:8080/myApp/search?words=aaa&words=bbb
.In this case, i was expected a printing as follow:
[aaa, bbb]
But actually, printed as follow:
[aaa]
In this case, request parameters can be bind to first element only.
In addition, i submit a request to
http://localhost:8080/myApp/search
. (without request parameters)In this case, printed as follow:
[word1, word2, word3]
In this case,default value converted by conversion service was applied.
This behavior is bug or specification ?
I will submit a repro project at the later.
Affects: 4.2.1
Issue Links:
@RequestParam
0 votes, 5 watchers
The text was updated successfully, but these errors were encountered: