将JSON字符串映射到POJO列表将得到空值

问题描述

我从rest api获取json,我想将数据存储在POJO列表中。下面是相同的代码:

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

我添加了FAIL_ON_UNKNOWN_PROPERTIES配置,因为与POJO相比,我在json中增加了额外的列,并且无法更改POJO(除非直到需要),因为我必须更改更多内容。我在下面的行中遇到异常时为对象映射器添加了ACCEPT_SINGLE_VALUE_AS_ARRAY配置:(我怀疑这是现在引起问题的原因)

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

这来自CollectionDeserializer.handleNonArray方法。

从rest api获取字符串的方法:

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","tradAcctType" : "01"
  } ]
  }

问题是:所有值都为null,列表的大小为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也可以使用。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...