首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于云配置服务器响应的Java模型

用于云配置服务器响应的Java模型
EN

Stack Overflow用户
提问于 2018-10-19 05:38:16
回答 1查看 31关注 0票数 0

我正在尝试为云配置服务器响应创建一个模型对象,以便在使用RestTemplate调用云配置服务器url时反序列化响应。当我使用很少的在线json到java生成器时,我看到生成的模型类似于下面所示。然而,"source“部分包含了键值对形式的所有属性,我想要一种通用的方法来反序列化它们。当我使用生成器时,它会生成一些特定于我在响应中的属性的东西吗?我怎样才能让它变得通用?

JSON Data

代码语言:javascript
复制
 {
   "name":"config",
   "profiles":[
      "dev"
   ],
   "label":null,
   "version":"b8379c098",
   "state":null,
   "propertySources":[
      {
         "name":"<url>/config-data/config-dev.properties",
         "source":{
            "cloud-switch":"on"
         }
      }
   ]

}

MyPojo

代码语言:javascript
复制
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

代码语言:javascript
复制
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+"]";
    }
}

代码语言:javascript
复制
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+"]";
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-19 05:58:31

将源变量设为Map,解决了这个问题。

代码语言:javascript
复制
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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52882957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档