用于云配置服务器响应的spring Java模型
我试图为cloud config server响应创建一个模型对象,以便在使用RestTemplate调用cloud config server url时对响应进行反序列化。当我在java生成器中使用很少的在线json时,我看到生成的模型与下面所示的类似。然而,“source”部分是以键值对的形式包含所有属性的部分,我想要一种反序列化它们的通用方法。当我使用生成器时,它生成了一些特定于我在响应中的属性的东西?我怎样才能使它通用
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+"]";
}
}
物业资源
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+"]";
}
}
# 1 楼答案
使源变量成为映射,解决了该问题