How do I send array parameter with Spring RestTemplate?
This is the server side implementation:
@RequestMapping(value = \"/train\", method = RequestM
Try this
Change your request mapping from
@RequestMapping(value = "/train", method = RequestMethod.GET)
to
@RequestMapping(value = "/train/{category}/{positiveDocId[]}/{negativeDocId[]}", method = RequestMethod.GET)
and your URL in restTemplate
change URl in the below given format
http://localhost:8080/admin/train/category/1,2,3,4,5/6,7,8,9
Here's how I've achieved it:
The REST Controller:
@ResponseBody
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ResponseEntity<Response> getPublicationSkus(
@RequestParam(value = "skus[]", required = true) List<String> skus) {
...
}
The request:
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);
Then you just need to implement a method that converts the List
or the String[]
to a plain string separated by commas, for example in Java 8
would be something like this:
private static String toPlainString(List<String> skus) {
return skus.stream().collect(Collectors.joining(","));
}
I ended up constructing the URL by looping through the collection.
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);
Spring's UriComponentsBuilder does the trick and allows also for Variable expansion. Assuming that you want to pass an array of strings as param "attr" to a resource for which you only have a URI with path variable:
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());
Otherwise, if you need to define a URI that is supposed to be expanded at runtime, and you do not know the size of the array in advance, use an http://tools.ietf.org/html/rfc6570 UriTemplate with {?key*} placeholder and expand it with the UriTemplate class from https://github.com/damnhandy/Handy-URI-Templates.
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);
For languages other than Java see https://code.google.com/p/uri-templates/wiki/Implementations.