简体   繁体   中英

Java Model for Cloud Config Server Response

I am trying to create a model object for the cloud config server response for me to deserialize the response when I use RestTemplate to call the cloud config server url. When I used few online json to java generators I see the model generated similar to what is shown below. However the "source" section is the one which contains all the properties in the form of key value pairs and I want a generic way to deserialize them. When I used the generator , it generated something specific to the property I had in the response? How can I make it generic?

JSON Data

 {
   "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+"]";
    }
}

Source

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+"]";
    }
}

Making source variable a Map , resolved the issue.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM