在我的 Spring Boot 项目中,我想通过连接几个字符串来创建一个 URL。我知道如何获得它们的第一个字符串。但是对于这些,我必须添加一些在地图中定义的字段。
但是如何从该映射中获取其键的列表,并为每个键添加键的名称及其各自的值?
这是我停止的代码部分:
@Value("${spring.route.source.protocol}")
private String protocol;
@Value("${spring.route.source.ip}")
private String ip;
@Value("${spring.route.source.root}")
private String root;
@Value("${spring.route.source.gets}")
private List<String> gets;
public void getWithRestTemplateGet1(Map<String, String> allGateAccessParams) {
final String methodName = "getWithRestTemplateGet1()";
try {
startLog(methodName);
List<String> keys = new ArrayList<String>();
//HOW CAN I ADD KEY E VALUE FROM MAP TO URL?
String url = protocol + ip + root + gets.get(0);
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
HttpEntity request = new HttpEntity(headers);
try {
if (url.startsWith("https")) {
restTemplate = getRestTemplateForSelfSsl();
} else {
restTemplate = new RestTemplate();
}
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
HttpStatus statusCode = response.getStatusCode();
logger.info("STATUS GET1: " + statusCode);
logger.info("URL GET1: " + url);
logger.info("RESPONSE GET1: " + response);
} catch (HttpStatusCodeException e) {
logger.error(e.getMessage());
}
endLog(methodName);
} catch (Exception e) {
logger.error(e.getMessage());
}
}
你能帮助我吗?
您可以entrySet()
从 map获取并迭代 entrySet 到 getKey usinggetKey()
和 value using getValue()
,如下所示
for (Map.Entry<String, String> entry : allGateAccessParams.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句