我正在尝试调用rest api并从api获取数据。我需要在 Spring Boot 中向 url 添加动态参数。我有点迷失,不知道该怎么办。有人可以给我建议吗?
RestTemplate restTemplate = new RestTemplate();
String consumeJSONString = restTemplate.getForObject("https://maps.googleapis.com/maps/api/geocode/json?latlng=5.47686,-73.961452&key=YOUR_API_KEY"
, String.class);
我想在 url 中动态附加 latlng 和 api key 。我真的很感激任何建议。
最佳答案
您必须使用 getForObject
方法的以下变体
restTemplate.getForObject(url, responseType, uriVariables);
所以就变成了..
String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng={latlng}&key={key}";
Map<String, Object> uriVariables = new HashMap<>();
uriVariables.put("latlng", "5.47686,-73.961452");
uriVariables.put("key", "YOUR_API_KEY");
String consumeJSONString = restTemplate.getForObject(url, String.class, uriVariables);
关于java - 在 Spring boot Java 中将参数附加到现有 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44967916/