如何使用jackson-annotations将值json映射到对象java

时间:2016-04-12 02:40:44

标签: java json jackson

我有一个字符串Json:

{
   "title": "PowerPoint Presentation",
   "author": "Hana",
   "subject": null,
   "keywords": null,
   "created_date": "2016-03-25 15:11:17",
   "modified_date": "2016-03-28 17:27:06",
   "creator": null,
   "producer": "LibreOffice 5.0",
   "pdfversion": null,
   "file_size": 149225,
   "total_page": 24
}

和Object java

public class ContentInfo {

    @JsonProperty("title")
    private String title;

    @JsonProperty("author")
    private String author;

    @JsonProperty("subject")
    private String subject;

    @JsonProperty("keywords")
    private String keywords;

    @JsonProperty("created_date")
    private String createdDate;

    @JsonProperty("modified_date")
    private String modifiedDate;
// (application name that create original file of PDF)

    @JsonProperty("creator")
    private String creator;
// (application name that create PDF)

    @JsonProperty("producer")
    private String producer;

    @JsonProperty("pdfversion")
    private String pdfversion;

    @JsonProperty("file_size")
    private long fileSize;

    @JsonProperty("total_page")
    private long totalPage;

    public ContentInfo() {
    }

    public ContentInfo(String title, String author, String subject, String keywords, String createdDate, String modifiedDate, String creator, String producer, String pdfversion, long fileSize, long totalPage, PageViewSetting page_view_setting) {
        this.title = title;
        this.author = author;
        this.subject = subject;
        this.keywords = keywords;
        this.createdDate = createdDate;
        this.modifiedDate = modifiedDate;
        this.creator = creator;
        this.producer = producer;
        this.pdfversion = pdfversion;
        this.fileSize = fileSize;
        this.totalPage = totalPage;
        this.page_view_setting = page_view_setting;
    }



    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getSubject() {
        return subject;
    }

    public String getKeywords() {
        return keywords;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public String getModifiedDate() {
        return modifiedDate;
    }

    public String getCreator() {
        return creator;
    }

    public String getProducer() {
        return producer;
    }

    public String getPdfversion() {
        return pdfversion;
    }

    public long getFileSize() {
        return fileSize;
    }

    public long getTotalPage() {
        return totalPage;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public void setModifiedDate(String modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public void setPdfversion(String pdfversion) {
        this.pdfversion = pdfversion;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }
}

我使用以下代码映射它们:

ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            data = objectMapper.readValue(this.jsonContentInfoData, ContentInfo.class);

然而,在某些领域,结果ResponseBody是错误的:

"content_info": {
"title": "PowerPoint Presentation"
"author": "Hana"
"subject": null
"keywords": null
"created_date": "2016-03-25 15:11:17"
"creator": null
"producer": "LibreOffice 5.0"
"pdfversion": null
"modified_date": "2016-03-28 17:27:06"
"file_size": 0
"total_page": 0
}

1 个答案:

答案 0 :(得分:0)

杰克逊根据您的字段的访问修饰符序列化和反序列化,并结合可用且适当命名的getter和setter方法。

您可以使用以下命令覆盖此功能以确保所有私有字段都是ser / deserialized:

mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

然而,它适合测试,但不是最佳解决方案。 相反,您应该在实践中使用私有字段并使用公共getter / setter来控制序列化和反序列化过程。

  
      
  • 公共Getter使非公共字段可序列化和可反序列化

         

    不直观地,getter也使私有字段也可以反序列化 - 因为一旦它有一个getter,该字段就被认为是一个属性。

  •   
  • 公众制定者只允许非公开字段反序列化

  •   

在您的代码中,显示getter / setter:

  1. setPdfversion - 不正确:应为setPdfVersion
  2. getTotalPage(long totalPage) - 不正确,旨在setTotalPage(long totalPage)
  3. 最后,我认为这有助于将totalPagefileSize的类型从原始long更改为包装器对象Long。同时更改这些字段的getter / setter以匹配Long类型。由于这两个字段都存在问题并且正在使用原语,因此Jackson(或您的版本)可能无法处理原语。