How to send array with Spring RestTemplate?

后端 未结 4 796
心在旅途
心在旅途 2021-01-14 06:43

How do I send array parameter with Spring RestTemplate?

This is the server side implementation:

@RequestMapping(value = \"/train\", method = RequestM         


        
4条回答
  •  猫巷女王i
    2021-01-14 07:40

    I ended up constructing the URL by looping through the collection.

    Map map = new HashMap();
    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);
    

提交回复
热议问题