![](/img/trans.png)
[英]How to set propertysource to config file fetched from cloud config server in java spring?
[英]Java Model for Cloud Config Server Response
我正在嘗試為雲配置服務器響應創建模型對象,以便在我使用RestTemplate調用雲配置服務器URL時反序列化響應。 當我使用很少的在線json到java生成器時,我看到生成的模型類似於下面所示。 但是,“源”部分是以鍵值對的形式包含所有屬性的部分,我希望有一種通用的方法來反序列化它們。 當我使用生成器時,它生成了特定於響應中的屬性的某些內容? 如何使它通用?
JSON數據
{
"name":"config",
"profiles":[
"dev"
],
"label":null,
"version":"b8379c098",
"state":null,
"propertySources":[
{
"name":"<url>/config-data/config-dev.properties",
"source":{
"cloud-switch":"on"
}
}
]
}
MyPojo
public class MyPojo
{
private PropertySources[] propertySources;
private String name;
private null state;
private null label;
private String[] profiles;
private String version;
public PropertySources[] getPropertySources ()
{
return propertySources;
}
public void setPropertySources (PropertySources[] propertySources)
{
this.propertySources = propertySources;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public null getState ()
{
return state;
}
public void setState (null state)
{
this.state = state;
}
public null getLabel ()
{
return label;
}
public void setLabel (null label)
{
this.label = label;
}
public String[] getProfiles ()
{
return profiles;
}
public void setProfiles (String[] profiles)
{
this.profiles = profiles;
}
public String getVersion ()
{
return version;
}
public void setVersion (String version)
{
this.version = version;
}
@Override
public String toString()
{
return "ClassPojo [propertySources = "+propertySources+", name = "+name+", state = "+state+", label = "+label+", profiles = "+profiles+", version = "+version+"]";
}
}
PropertySources
public class PropertySources
{
private Source source;
private String name;
public Source getSource ()
{
return source;
}
public void setSource (Source source)
{
this.source = source;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
@Override
public String toString()
{
return "ClassPojo [source = "+source+", name = "+name+"]";
}
}
資源
public class Source
{
private String cloud-switch;
public String getCloud-switch ()
{
return cloud-switch;
}
public void setCloud-switch (String cloud-switch)
{
this.cloud-switch = cloud-switch;
}
@Override
public String toString()
{
return "ClassPojo [cloud-switch = "+cloud-switch+"]";
}
}
將源變量設為Map,解決了該問題。
public class PropertySource {
private String name;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
private Map<String, String> source;
public Map<String, String> getSource() {
return source;
}
public void setSource(Map<String, String> source) {
this.source = source;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.