如何使用 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);
以下是明显不正确的实际请求 URL:
http://localhost:8080/admin/train?category=spam&positiveDocId%5B%5D=%5BLjava.lang.String;@4df2868&negativeDocId%5B%5D=%5BLjava.lang.String;@56d5c657`
一直在尝试四处搜索,但找不到解决方案。任何指针将不胜感激。