将 JSON 字符串映射为 POJO 列表会导致空值。

huangapple go评论64阅读模式
英文:

Mapping JSON String to List of POJO gives null values

问题

以下是代码的翻译部分:

public List<myObject> mapper(){

    String myObjectData = restClient.getAllOriginal("myObject");

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

    CollectionType typeReference =
            TypeFactory.defaultInstance().constructCollectionType(List.class, myObject.class);
    List<CommitmentPojo> resultDto = null;

    try
    {

         resultDto = objectMapper.readValue(myObjectData, typeReference);
        
    }
    catch (JsonParseException e)
    {
        e.printStackTrace();
    }
    catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return resultDto;
}

// ... 其他方法 ...

public  String getAllOriginal(String resourcePath) {
       // Objects.requireNonNull(this.baseUri, "target cannot be null");
        return this.client
                .target("http://comtsrvc.ny.qa.flx.nimbus.gs.com:3802/v2/")
                .path(resourcePath)
                .request(MediaType.APPLICATION_JSON_TYPE)
                .cookie("GSSSO", getCookie())
                .get()
                .readEntity(String.class);
    }

以下是 JSON 的翻译部分:

{
  "myObject" : [ {
    "key" : {
      "srcSys" : "REPO_1",
      "srcSysRef" : "20200909_1911_1"
    },
    "productData" : {
      "id" : null,
      "number" : null,
      "isn" : null,
      "productId" : null,
      "productAdditionalData" : {
        "assetClassTree" : "UNCLASSIFIED",
        "description" : "UNCLASSIFIED",
        "productTypeData" : {
          "productType" : "UNCLASSIFIED",
          "productGroup" : "UNCLASSIFIED"
        }
      }
    },
    "state" : "OPEN",
    "type" : "01"
  }, {
    "key" : {
      "srcSys" : "REPO_2",
      "srcSysRef" : "20200403_3892_1"
    },
    "productData" : {
      "id" : "1",
      "number" : "11",
      "isn" : "null",
      "productId" : 1234,
      "productAdditionalData" : {
        "assetClassTree" : "xyz",
        "description" : "abc",
        "productTypeData" : {
          "productType" : "UNCLASSIFIED",
          "productGroup" : "UNCLASSIFIED"
        }
      }
    },
    "state" : "OPEN",
    "tradAcctType" : "01"
  } ]
}

由于您要求只返回翻译好的部分,我已经按照您的要求提供了上述内容的翻译。如果您有任何进一步的问题或需要解释,请随时提问。

英文:

I'm getting json from rest api and I want to store the data in list of POJO. Below is the codefor the same:

public List&lt;myObject&gt; mapper(){

    String myObjectData= restClient.getAllOriginal(&quot;myObject&quot;);

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

    CollectionType typeReference =
            TypeFactory.defaultInstance().constructCollectionType(List.class, myObject.class);
    List&lt;CommitmentPojo&gt; resultDto = null;

    try
    {

         resultDto = objectMapper.readValue(myObjectData, typeReference);
        
    }
    catch (JsonParseException e)
    {
        e.printStackTrace();
    }
    catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return resultDto;
}

I've added FAIL_ON_UNKNOWN_PROPERTIES configuration as I've extra columns in json as compared to POJO and I can't change POJO(unless and until required) as I'll have to change many more things. I've added ACCEPT_SINGLE_VALUE_AS_ARRAY configuration for object mapper as I was facing exception in below line: (I suspect this is causing the issue now)

// [JACKSON-526]: implicit arrays from single values?
        if (!ctxt.isEnabled(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) {
            throw ctxt.mappingException(_collectionType.getRawClass());
        }

This is from CollectionDeserializer.handleNonArray method.

Method which gets the string from rest api:

public  String getAllOriginal(String resourcePath) {
       // Objects.requireNonNull(this.baseUri, &quot;target cannot be null&quot;);
        return this.client
                .target(&quot;http://comtsrvc.ny.qa.flx.nimbus.gs.com:3802/v2/&quot;)
                .path(resourcePath)
                .request(MediaType.APPLICATION_JSON_TYPE)
                .cookie(&quot;GSSSO&quot;, getCookie())
                .get()
                .readEntity(String.class);
    }

Below is my json:

{
  
  &quot;myObject&quot; : [ {
    &quot;key&quot; : {
      &quot;srcSys&quot; : &quot;REPO_1&quot;,
      &quot;srcSysRef&quot; : &quot;20200909_1911_1&quot;
    },
    &quot;productData&quot; : {
      &quot;id&quot; : null,
      &quot;number&quot; : null,
      &quot;isn&quot; : null,
      &quot;productId&quot; : null,
      &quot;productAdditionalData&quot; : {
        &quot;assetClassTree&quot; : &quot;UNCLASSIFIED&quot;,
        &quot;description&quot; : &quot;UNCLASSIFIED&quot;,
        &quot;productTypeData&quot; : {
          &quot;productType&quot; : &quot;UNCLASSIFIED&quot;,
          &quot;productGroup&quot; : &quot;UNCLASSIFIED&quot;
        }
      }
    },
    &quot;state&quot; : &quot;OPEN&quot;,
    &quot;type&quot; : &quot;01&quot;
  }, {
    &quot;key&quot; : {
      &quot;srcSys&quot; : &quot;REPO_2&quot;,
      &quot;srcSysRef&quot; : &quot;20200403_3892_1&quot;
    },
    &quot;productData&quot; : {
      &quot;id&quot; : &quot;1&quot;,
      &quot;number&quot; : &quot;11&quot;,
      &quot;isn&quot; : &quot;null&quot;,
      &quot;productId&quot; : 1234,
      &quot;productAdditionalData&quot; : {
        &quot;assetClassTree&quot; : &quot;xyz&quot;,
        &quot;description&quot; : &quot;abc&quot;,
        &quot;productTypeData&quot; : {
          &quot;productType&quot; : &quot;UNCLASSIFIED&quot;,
          &quot;productGroup&quot; : &quot;UNCLASSIFIED&quot;
        }
      }
    },
    &quot;state&quot; : &quot;OPEN&quot;,
    &quot;tradAcctType&quot; : &quot;01&quot;
  } ]
  }

The issue is: all the values are null with the size of list as 1. Can you please tell me what is wrong with my code.

答案1

得分: 1

尝试将其反序列化为 Map

import com.fasterxml.jackson.core.type.TypeReference;

...

Map<String, List<MyObject>> root = mapper.readValue(jsonFile, new TypeReference<Map<String, List<MyObject>>>() {});
List<MyObject> objects = root.get("myObject");

因此,您无需为根级别创建新的 POJOMap 也可以使用。

英文:

Try to deserialize it to a Map:

import com.fasterxml.jackson.core.type.TypeReference;

...

Map&lt;String, List&lt;MyObject&gt;&gt; root = mapper.readValue(jsonFile, new TypeReference&lt;Map&lt;String, List&lt;MyObject&gt;&gt;&gt;() {});
List&lt;MyObject&gt; objects = root.get(&quot;myObject&quot;);

So you do not need to create a new POJO for a root level. Map will also work.

huangapple
  • 本文由 发表于 2020年9月21日 18:23:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63990417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定