为空属性获取com.fasterxml.jackson.databind.JsonMappingException

时间:2018-11-09 18:22:15

标签: java json jackson

我得到了com.fasterxml.jackson.databind.JsonMappingException甚至使用@JsonInclude(JsonInclude.Include.NON_EMPTY)和@JsonIgnoreProperties(ignoreUnknown = true)注释类。

消息如下:

java.lang.NullPointerException)(通过参考链:com.somepackage.Whatever [“ data”]-> java.util.ArrayList [4]-> com.somepackage.transport.Foo [“ assets”]-> com.somepackage.transport.Blah [“ htmls”])

下面是我的代码的组织方式的想法:

public class Whatever<T> {
  /**
   * Response data of pagination
   */
  @JsonProperty("data")
  private List<T> data;

  /**
   * Data Structure which provide the pagination structures for collection
   */
  private Pagination pagination;


}

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {

  ...

  @JsonProperty("assets")
  public Blah getAssets() {
    return adaptee != null ? new Blah(adaptee.getAssets()) : null;
  }
  
  ...

}

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Blah {

  ...
  
  @JsonProperty("htmls")
  public Set<BlahBlah> getHtmls() {
    Set<Assets.Html> ihtmls = assetEntity.getHtmls();
    if (ihtmls == null) {
      return new HashSet<>();
    }
    Set<BlahBlah> htmls = null;
    if (!ihtmls.isEmpty()) {
      htmls = new HashSet<>();
      for (Assets.Html iHtml : ihtmls) {
        BlahBlah blahBlah = new BlahBlah(iHtml);
        htmls.add(blahBlah);
      }
    }
    return htmls;
  }
  
  ...

}

我使用杰克逊2.9.0到2.9.7版本进行了测试,但是遇到了同样的问题。我一年前发现了这个conversation,这是关于反序列化的类似问题。

有没有解决方法或解决了我的问题?也许我遗漏了一些东西。

0 个答案:

没有答案