这样:
@RequestMapping("/foo")
public String foo(@RequestParam(value="test", defaultValue="foo")
String t) {
return t;
}
我可以使用curl localhost:8080/foo?test=helloWorld
调用我的函数,它将返回helloWorld
。但是,我需要创建一个 API,其中可以给出相同的参数 x 次:
curl localhost:8080/foo?test=helloWorld&test=helloAgain&test=oneMoreTime
所以我们开始:
@RequestMapping("/foo")
public String foo(@RequestParam(value="test", defaultValue="foo")
String[] t) {
return t[1]; // return helloAgain
}
现在我可以通过更改索引来获取所有三个值。这很好用,但我有一个简单的问题:如何选择数组长度?我不知道我会收到多少次相同的参数。
预先感谢您的帮助。
请您参考如下方法:
如果您希望在 @RequestParam 中使用相同的参数 x 次,您可以使用数组(如第二个示例),但您不能“选择”(定义)那里的大小,但您可以获取长度。正确的语法是这个:
@RequestMapping("/foo")
public String foo(@RequestParam(value="test") String[] t) {
Integer size = str.length;
return t[1]; // return helloAgain
}