2017-07-18 106 views
1

我需要在yml屬性中使用Map<String, Object>,但允許在空值中輸入密鑰。如何使用鍵和空值在yml中定義映射?

例如:

application.yml

.... 
init-services: 
    sample-service-discovery: 
    port: 8761 
    profiles: dev,prod 
    sample-config-server: 
    port: 8888 
    profiles: dev, test 
    sample-activation-service: 
    some-other-service: 
    port: 1234 
.... 

init-servicesMap<String, ServiceProperties>

ServiceProperties只是一個POJO與Integer port, List<String> profiles

它編好了前2項,但在我得到Cannot convert value of type 'java.lang.String' to required type PathProperties$ServiceProperties異常。

問:是否有任何方法允許在.yml映射中只有一個鍵但空值爲空的條目?我需要稍後迭代密鑰,即使它們的值爲空。

+0

你可以有一個'null' YAML的密鑰,但這使得它成爲一個非字符串標量。你確定你想要「空鍵值的字符串鍵」,看起來像是對我來說最終的矛盾。 – Anthon

+0

我不想要一個空鍵,我想要一個鍵的空值。因此,如果我在代碼中執行:'map.get(「sample-activation-service」)'它返回'null'。如果我的措辭不夠清晰,我願意接受建議/編輯。我編輯了這個問題,試圖說清楚。讓我知道它是否有幫助。 – maydawn

+0

對不起,我不知道如何使用Java生成。但是'{null:1,True:x,x:42}'是有效的YAML。當然,任何映射中只能有一個'null'鍵,因爲映射的鍵必須是唯一的。 – Anthon

回答

1

我設法解決了這個問題,方法是在ServiceProperties中添加一個構造函數,它將String作爲參數,並調用默認構造函數。

public ServiceProperties(String empty) {this();} 
相關問題