我以set的形式使用param创建端点:
@GetMapping("/me")
public MeDto getInfo(@RequestParam("param") Set<Integer> params) {
...
}一切都很好,但我需要依次发送I。
/me?param=1¶m=2有没有办法使它成为:
/me?param=1,2...N有什么想法吗?谢谢。
发布于 2018-10-28 11:10:51
你可以做这样的事
@RequestMapping(method=RequestMethod.GET, value="/me")
public ResponseEntity<?> getValues(@RequestParam String... param){
Set<String> set= new TreeSet<String>(Arrays.asList(param));
return new ResponseEntity<Set>(set, HttpStatus.OK);
}所以如果你点击-> localhost:8786/me?param=hello,foo,bar,动物,你会得到以下响应
“动物”,“酒吧”,“福”,“你好”
发布于 2018-10-28 11:06:40
好的,我在start.spring.io上创建的一个新的"Spring环境“中测试了它
正如注释中已经提到的那样,它的工作方式是开箱即用的,但只适用于整数数组(而不是一组)。如果您要使用列出的选项之一,您可以使用Set<Integer> ints = Arrays.stream(params).collect(Collectors.toSet())删除重复的数字(我想这是您使用集合的目的)。
当绝对没有“空”号时:
@GetMapping("/intarray")
public Object someGetMapping(int[] params){
return params;
}http://localhost:8080/api/intarray?params=1,2,3,4,5,3
输出(如预期的整数数组):
[
1,
2,
3,
4,
5,
3]
如果其中可能有一个空的数字,我建议使用Integer作为数组。
@GetMapping("/intset")
public Object someOtherGetMapping(Integer[] params){
return params;
}http://localhost:8080/api/intset?params=1,2,3,4,5,,,5
输出(带有空值,因为查询中有空字段):
[
1,
2,
3,
4,
5,
null,
null,
5]
https://stackoverflow.com/questions/53029982
复制相似问题