简体   繁体   中英

Jackson missing out values

I am trying to map a JSON to a simple Java DTO.
Here is my Java structure:

public class VirtualServerResponse {
    private String kind;
    private String selfLink;
    private List<VirtualServer> items = new ArrayList<VirtualServer>();
    //no arg constructor
    //getters and setters

@JsonIgnoreProperties(ignoreUnknown = true)
public class VirtualServer {
    public String kind;
    public String name;
    public String partition;
    public String fullPath;
    public String generation;
    public String selfLink;
    public String addressStatus;
    public String autoLasthop;
    public String cmpEnabled;
    public String connectionLimit;
    public String description;
    public String destination;
    public String enabled;
    public String gtmScore;
    public String ipProtocol;
    public String mask;
    public String mirror;
    public String mobileAppTunnel;
    public String nat64;
    public String pool;
    public String rateLimit;
    public String rateLimitDstMask;
    public String rateLimitMode;
    public String rateLimitSrcMask;
    public String serviceDownImmediateAction;
    public String source;
    public String sourcePort;
    public String synCookieStatus;
    public String translateAddress;
    public String translatePort;
    public String vlansEnabled;
    public String vsIndex;
    public PoolDTO assignedPool;

    public VirtualServer() {
    }
    //getters and setters

Here is the JSON that should be mapped:

"kind":"tm:ltm:virtual:virtualcollectionstate",
   "selfLink":"https://localhost/mgmt/tm/ltm/virtual?expandSubcollections=true&ver=13.1.1.2",
   "items":[
      {
         "kind":"tm:ltm:virtual:virtualstate",
         "name":"some_name_with:80",
         "partition":"part",
         "fullPath":"/part/name",
         "generation":58670,
         "selfLink":"https://localhost/mgmt/tm/ltm/virtual/~somelink",
         "addressStatus":"yes",
         "autoLasthop":"default",
         "cmpEnabled":"yes",
         "connectionLimit":0,
         "description":"description",
         "destination":"/part/1.1.1.1:80",
         "enabled":true,
         "gtmScore":0,
         "ipProtocol":"tcp",
         "mask":"255.255.255.255",
         "mirror":"disabled",
         "mobileAppTunnel":"disabled",
         "nat64":"disabled",
         "pool":"/pool",
         "poolReference":{
            "link":"https://localhost/mgmt/tm/ltm/pool/link"
         },
         "rateLimit":"disabled",
         "rateLimitDstMask":0,
         "rateLimitMode":"object",
         "rateLimitSrcMask":0,
         "serviceDownImmediateAction":"none",
         "source":"0.0.0.0/0",
         "sourceAddressTranslation":{
            "type":"automap"
         },
         "sourcePort":"preserve",
         "synCookieStatus":"not-activated",
         "translateAddress":"enabled",
         "translatePort":"enabled",
         "vlansEnabled":true,
         "vsIndex":137,
         "vlans":[
            "/LAN"
         ],
         "vlansReference":[
            {
               "link":"https://localhost/mgmt/tm/net/vlan/~LAN?ver=13.1.1.2"
            }
         ],
         "policiesReference":{
            "link":"https://localhost/mgmt/tm/ltm/virtual/policie",
            "isSubcollection":true
         },
         "profilesReference":{
            "link":"https://localhost/mgmt/tm/ltm/virtual/~name",
            "isSubcollection":true,
            "items":[
               {
                  "kind":"tm:ltm:virtual:profiles:profilesstate",
                  "name":"stats",
                  "partition":"part",
                  "fullPath":"/part/stats",
                  "generation":3,
                  "selfLink":"https://localhost/mgmt/tm/ltm/virtual/~name",
                  "context":"all",
                  "nameReference":{
                     "link":"https://localhost/mgmt/tm/ltm/profile/statistics/~part~stats?ver=13.1.1.2"
                  }
               },
               {
                  "kind":"tm:ltm:virtual:profiles:profilesstate",
                  "name":"tcp",
                  "partition":"part",
                  "fullPath":"/part/tcp",
                  "generation":58670,
                  "selfLink":"https://localhost/mgmt/tm/ltm/virtual/~name",
                  "context":"all",
                  "nameReference":{
                     "link":"https://localhost/mgmt/tm/ltm/profile/tcp/~part~tcp?ver=13.1.1.2"
                  }
               }
            ]
         }
      }, ... next item

The JSON is mapped by the whole JSON in one line:

while ((line = br.readLine()) != null) {
    this.jsonResponse = m.readValue(line, VirtualServerResponse.class);
}

I do not need all the subitems in a item , so I used JsonIgnoreUnknown to let them off. However, there are only a few properties, that are mapped:
kind, name, partition, fullPath, generation, selfLink and description.

All others are null. Can someone help me?

It seems that there are properties at different levels.

You have mapped an object with all properties directly under the root, so all other nested properties are not visible.

If the json is something like:

{
   "kind":"tm:ltm:virtual:virtualcollectionstate",
   "selfLink":"https://localhost/mgmt/tm/ltm/virtual?expandSubcollections=true&ver=13.1.1.2",
   "items":[
      {
        "kind":"tm:ltm:virtual:virtualstate",
        "name":"some_name_with:80",
        "partition":"part",
        "fullPath":"/part/name",
        ...
      }
   ]
}

You need to map it to an object similar to:

public class VirtualServer {
   private String kind;
   private String selfLink;
   private List<VirtualServer.Item> items;
   ...

   public static class Item {
       private String kind;
       private String name;
       private String partition;
       private String fullPath;

       ...
   }
}

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