Java 如何使用Spring RestTemplate发送阵列?

Java 如何使用Spring RestTemplate发送阵列?,java,spring,rest,Java,Spring,Rest,如何使用Spring RestTemplate发送数组参数 这是服务器端实现: @RequestMapping(value = "/train", method = RequestMethod.GET) @ResponseBody public TrainResponse train(Locale locale, Model model, HttpServletRequest request, @RequestParam String category, @RequestPar

如何使用Spring RestTemplate发送数组参数

这是服务器端实现:

@RequestMapping(value = "/train", method = RequestMethod.GET)
@ResponseBody
public TrainResponse train(Locale locale, Model model, HttpServletRequest request, 
    @RequestParam String category,
    @RequestParam(required = false, value = "positiveDocId[]") String[] positiveDocId,
    @RequestParam(required = false, value = "negativeDocId[]") String[] negativeDocId) 
{
    ...
}
这就是我尝试过的:

Map<String, Object> map = new HashMap<String, Object>();
map.put("category", parameters.getName());
map.put("positiveDocId[]", positiveDocs); // positiveDocs is String array
map.put("negativeDocId[]", negativeDocs); // negativeDocs is String array
TrainResponse response = restTemplate.getForObject("http://localhost:8080/admin/train?category={category}&positiveDocId[]={positiveDocId[]}&negativeDocId[]={negativeDocId[]}", TrainResponse.class, map);
一直在尝试四处搜索,但找不到解决方案。任何提示都将不胜感激。

试试这个

从更改请求映射

@RequestMapping(value = "/train", method = RequestMethod.GET)

以及restTemplate中的URL

更改以下给定格式的URl

http://localhost:8080/admin/train/category/1,2,3,4,5/6,7,8,9

我最终通过在集合中循环来构建URL

Map<String, Object> map = new HashMap<String, Object>();
map.put("category", parameters.getName());

String url = "http://localhost:8080/admin/train?category={category}";
if (positiveDocs != null && positiveDocs.size() > 0) {
    for (String id : positiveDocs) {
        url += "&positiveDocId[]=" + id;
    }
}
if (negativeDocId != null && negativeDocId.size() > 0) {
    for (String id : negativeDocId) {
        url += "&negativeDocId[]=" + id;
    }
}

TrainResponse response = restTemplate.getForObject(url, TrainResponse.class, map);
Map Map=newhashmap();
put(“category”,parameters.getName());
字符串url=”http://localhost:8080/admin/train?category={类别}”;
if(positiveDocs!=null&&positiveDocs.size()>0){
for(字符串id:positiveDocs){
url+=“&positiveDocId[]=”+id;
}
}
if(negativeDocId!=null&&negativeDocId.size()>0){
for(字符串id:negativeDocId){
url+=“&negativeCID[]=”+id;
}
}
TrainResponse=restTemplate.getForObject(url、TrainResponse.class、map);

Spring的UriComponentsBuilder实现了这一功能,还允许变量扩展。假设您希望将字符串数组作为param“attr”传递给一个资源,该资源只有一个带有path变量的URI:

UriComponents comp = UriComponentsBuilder.fromHttpUrl(
    "http:/www.example.com/widgets/{widgetId}").queryParam("attr", "width", 
        "height").build();
UriComponents expanded = comp.expand(12);
assertEquals("http:/www.example.com/widgets/12?attr=width&attr=height", 
   expanded.toString());
否则,如果需要定义一个应该在运行时展开的URI,并且事先不知道数组的大小,请使用带有{?key*}占位符的UriTemplate,并使用中的UriTemplate类展开它


对于Java以外的语言,请参见。

以下是我如何实现的:

REST控制器:

@ResponseBody
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ResponseEntity<Response> getPublicationSkus(
       @RequestParam(value = "skus[]", required = true) List<String> skus) {
    ...
}
UriComponents comp = UriComponentsBuilder.fromHttpUrl(
    "http:/www.example.com/widgets/{widgetId}").queryParam("attr", "width", 
        "height").build();
UriComponents expanded = comp.expand(12);
assertEquals("http:/www.example.com/widgets/12?attr=width&attr=height", 
   expanded.toString());
UriTemplate template = UriTemplate.fromTemplate(
    "http://example.com/widgets/{widgetId}{?attr*}");
template.set("attr", Arrays.asList(1, 2, 3));
String expanded = template.expand();
assertEquals("http://example.com/widgets/?attr=1&attr=2&attr=3", 
    expanded);
@ResponseBody
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ResponseEntity<Response> getPublicationSkus(
       @RequestParam(value = "skus[]", required = true) List<String> skus) {
    ...
}
List<String> skus = Arrays.asList("123","456","789");

Map<String, String> params = new HashMap<>();
params.put("skus", toPlainString(skus));

Response response = restTemplate.getForObject("http://localhost:8080/test?skus[]={skus}", 
                                      Response.class, params);
private static String toPlainString(List<String> skus) {
    return skus.stream().collect(Collectors.joining(","));
}