Spring HTTP 接口 - 逗号分隔的参数值列表

问题描述 投票:0回答:1

我在春天有以下

@HttpExchange

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;

...

    @GetExchange("/foo/bar")
    List<Baz> items(
            @RequestParam(name = "ids") List<String> ids
    );

当我执行它时,

ids
像这样发送:

ids=1&ids=2&...

但是,我需要将它们发送如下:

ids=1,2,...

我想知道是否有 Spring 本机方式将它们作为逗号分隔值发送。

目前,当我调用这个方法时,我需要通过以下方式准备ID:

String.join(",", ids);
java spring spring-boot
1个回答
0
投票

您可以简单地将 String 作为输入,并使用 split() 函数来获取所有 id。

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;

@GetExchange("/foo/bar")
List<Baz> items(
        @RequestParam(name = "ids") String idStr
) {
   List<String> ids = idStr.split(",");
   ...
  ;
© www.soinside.com 2019 - 2024. All rights reserved.