如何在Groovy中将Map Key的值赋给字符串

时间:2017-06-16 08:04:00

标签: groovy hashmap key

我写了一个返回Map的方法(如下所述)。

Class MyTest
static Map<String, String> getApplicationPreferences() {
    String host = System.getProperty("property1")
    String namespace = System.getProperty("property2")
    String username = System.getProperty("property3")
    String password = System.getProperty("property4")
    String url = "http://" + host + ":10000/v3/namespaces/" + namespace + "/apps/MyApplication/preferences"
    String token = RestUtil.getToken(host, username, password)
    HttpURLConnection connection = RestUtil.sendRequest(url, token, true, RestUtil.REQUEST_METHOD.GET)
    String response = connection.getInputStream().getText()
    Map<String, String> preferences = new ObjectMapper().readValue(response, Map.class)
    return preferences
}

我将上述方法称为:

static Map<String, String> envPreferences = new HashMap<String, String>();
envPreferences = MyTest.getApplicationPreferences()

并按照以下方式从地图打印值:

println("\nAll preferences="+envPreferences.toString()+"\n")

现在我想将值赋给String变量,我必须传递给另一个函数。

static String a = envPreferences.get('messaging.kafkaBrokers')
println("Kafka preferences="+a)

static String b = envPreferences.get("zookeeper.server").toString()
println("Zookeeper preferences="+b)

我得到的上述印刷语句的输出是:

All preferences=[zookeeper.server:localhost:2181, messaging.kafkaBrokers:localhost:9092]
Kafka preferences=null
Zookeeper preferences="null"

如何获取值而不是null?
提前致谢。

0 个答案:

没有答案