简体   繁体   中英

how to map value json to object java using jackson-annotations

I have a string 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
}

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

I am using the below code to map them :

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

However result ResponseBody is wrong in some field:

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

Jackson serializes and deserializes based on the access modifier of your field, in conjunction with the available and properly named getter and setter method.

You can override this feature to insure all private fields are ser/deserialized using:

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

However, it's decent for testing, but isn't the optimal solution. Instead, you should in practice use private fields and use public getters/setters to control the serialization and deserialization process.

  • A public Getter Makes a Non-Public Field Serializable and Deserializable

    Unintuitively, the getter also makes the private field deserializable as well – because once it has a getter, the field is considered a property.

  • A public Setter Makes a Non-Public Field Deserializable Only

In your code, showing the getters/setters:

  1. setPdfversion - Is incorrect: Should be setPdfVersion
  2. getTotalPage(long totalPage) - Is incorrect and is intended to be setTotalPage(long totalPage)

Finally, I think it will help to change the types of totalPage and fileSize from the primitive long to the wrapper object Long . Also change the getter/setters for these fields to match the Long types. Since both of these fields are having issues and are using primitives, it seems possible that Jackson (or your version of it) does not handle primitives.

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