使用Jackson库对象Map器将JSON对象Map到Java类

mdfafbf1  于 2022-12-24  发布在  Java
关注(0)|答案(2)|浏览(190)

我有一个如下所示的JSON

{
   "resourceType": "PaymentCapture",
   "market": "XX",
   "brand": "XX",
   "channel": "XXX",
   "currency": "XXX",
   "version": "1",
   "eventName": "XXXXXXXX",
   "invoice": {
      "new": "XXXX",
      "old": ""
   },
   "tenders": [
      {
         "status": "XXXXXXXX",
         "links": [
            {
               "rel": "XXXXXXX",
               "href": "XXXX"
            },
            {
               "rel": "XXXXXX",
               "href": "XXXX"
            }
         ]
      }
   ],
   "publishedTime": "2022-12-23T01:00:01-0500",
   "retentionPolicy": "1"
}

我想读取它并使用Jackson库将其加载到Java对象中。
我这样定义类

package com.teamx;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.ArrayList;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class InvoicePayment {
    @JsonProperty("resourceType")
    private String resourceType;

    @JsonProperty("market")
    private String market;

    @JsonProperty("brand")
    private String brand;

    @JsonProperty("channel")
    private String channel;

    @JsonProperty("currency")
    private String currency;

    @JsonProperty("version")
    private String version;

    @JsonProperty("eventName")
    private String eventName;

    @JsonProperty("invoice")
    private Invoice invoice; // inner class

    @JsonProperty("tenders")
    private List<Tender> tenders = new ArrayList<>();

    @JsonProperty("publishedTime")
    private String publishedTime;

    @JsonProperty("retentionPolicy")
    private String retentionPolicy;

    public String getResourceType() {
        return resourceType;
    }

    // Inner class for invoice object
    class Invoice {

        @JsonProperty("new")
        private String _new;
        @JsonProperty("old")
        private String old;

        public String get_new() {
            return _new;
        }

        public String getOld() {
            return old;
        }

        public void set_new(String _new) {
            this._new = _new;
        }

        public void setOld(String old) {
            this.old = old;
        }
    }
    // tender class for tender array
    class Tender {

        @JsonProperty("status")
        private String status;

        @JsonProperty("links")
        private List<Link> links = new ArrayList<Link>();

        class Link {

            @JsonProperty("rel")
            private String rel;

            @JsonProperty("href")
            private String href;

            public String getRel() {
                return rel;
            }

            public void setRel(String rel) {
                this.rel = rel;
            }

            public String getHref() {
                return href;
            }

            public void setHref(String href) {
                this.href = href;
            }
        }
        public String getStatus() {
            return status;
        }

        public List<Link> getLinks() {
            return links;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public void setLinks(List<Link> links) {
            this.links = links;
        }
    }

    public void setResourceType(String resourceType) {
        this.resourceType = resourceType;
    }

    public String getMarket() {
        return market;
    }

    public void setMarket(String market) {
        this.market = market;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public Invoice getInvoice() {
        return invoice;
    }

    public void setInvoice(Invoice invoice) {
        this.invoice = invoice;
    }

    public List<Tender> getTenders() {
        return tenders;
    }

    public void setTenders(List<Tender> tenders) {
        this.tenders = tenders;
    }

    public String getPublishedTime() {
        return publishedTime;
    }

    public void setPublishedTime(String publishedTime) {
        this.publishedTime = publishedTime;
    }

    public String getRetentionPolicy() {
        return retentionPolicy;
    }

    public void setRetentionPolicy(String retentionPolicy) {
        this.retentionPolicy = retentionPolicy;
    }

    @Override
    public String toString() {
        return "InvoicePayment{" +
                "resourceType='" + resourceType + '\'' +
                ", market='" + market + '\'' +
                ", brand='" + brand + '\'' +
                ", channel='" + channel + '\'' +
                ", currency='" + currency + '\'' +
                ",Invoice.new '"+ invoice.get_new() + '\'' +
                ", version='" + version + '\'' +
                ", eventName='" + eventName + '\'' +
                ", publishedTime='" + publishedTime + '\'' +
                ", retentionPolicy='" + retentionPolicy + '\'' +
                '}';
    }
}

当我运行这个程序时,我得到了一个异常,如下所示,我做错了什么?

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.teamx.InvoicePayment$Tender`: non-static inner classes like this can only by instantiated using default, no-argument constructor
 at [Source: (File); line: 1, column: 326] (through reference chain: com.teamx.InvoicePayment["tenders"]->java.util.ArrayList[0])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
    at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1615)
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1072)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1329)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:331)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:164)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:290)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:249)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:293)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:156)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4526)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3333)
    at com.teamx.Main.readJsonandMaptoObj(Main.java:68)
    at com.teamx.Main.main(Main.java:14)

Process finished with exit code 0
rjjhvcjd

rjjhvcjd1#

无法构造com.teamx.InvoicePayment$Tender的示例:像这样的非静态内部类只能使用默认的无参数构造函数进行示例化
这意味着Jackson不能示例化Invoice类,因为作为非静态的结果,它需要一个封闭类的示例(而构建InvoicePayment又需要Invoice的示例)。
因此,您需要将static修饰符应用于Invoice类,同样的方法也适用于Tender类。

2g32fytz

2g32fytz2#

Maven依赖

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.14.1</version>
</dependency>

第一个月

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public class Tender {

    @JsonProperty("market")
    private String market;

    @JsonProperty("publishedTime")
    private String publishedTime;

    @JsonProperty("tenders")
    private List<TendersItem> tenders;

    @JsonProperty("channel")
    private String channel;

    @JsonProperty("eventName")
    private String eventName;

    @JsonProperty("currency")
    private String currency;

    @JsonProperty("invoice")
    private Invoice invoice;

    @JsonProperty("retentionPolicy")
    private String retentionPolicy;

    @JsonProperty("brand")
    private String brand;

    @JsonProperty("version")
    private String version;

    @JsonProperty("resourceType")
    private String resourceType;

    public void setMarket(String market) {
        this.market = market;
    }

    public String getMarket() {
        return market;
    }

    public void setPublishedTime(String publishedTime) {
        this.publishedTime = publishedTime;
    }

    public String getPublishedTime() {
        return publishedTime;
    }

    public void setTenders(List<TendersItem> tenders) {
        this.tenders = tenders;
    }

    public List<TendersItem> getTenders() {
        return tenders;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getChannel() {
        return channel;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public String getEventName() {
        return eventName;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getCurrency() {
        return currency;
    }

    public void setInvoice(Invoice invoice) {
        this.invoice = invoice;
    }

    public Invoice getInvoice() {
        return invoice;
    }

    public void setRetentionPolicy(String retentionPolicy) {
        this.retentionPolicy = retentionPolicy;
    }

    public String getRetentionPolicy() {
        return retentionPolicy;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getVersion() {
        return version;
    }

    public void setResourceType(String resourceType) {
        this.resourceType = resourceType;
    }

    public String getResourceType() {
        return resourceType;
    }

    @Override
    public String toString() {
        return
                "Response{" +
                        "market = '" + market + '\'' +
                        ",publishedTime = '" + publishedTime + '\'' +
                        ",tenders = '" + tenders + '\'' +
                        ",channel = '" + channel + '\'' +
                        ",eventName = '" + eventName + '\'' +
                        ",currency = '" + currency + '\'' +
                        ",invoice = '" + invoice + '\'' +
                        ",retentionPolicy = '" + retentionPolicy + '\'' +
                        ",brand = '" + brand + '\'' +
                        ",version = '" + version + '\'' +
                        ",resourceType = '" + resourceType + '\'' +
                        "}";
    }
}

TendersItem.java

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public class TendersItem{

    @JsonProperty("links")
    private List<LinksItem> links;

    @JsonProperty("status")
    private String status;

    public void setLinks(List<LinksItem> links){
        this.links = links;
    }

    public List<LinksItem> getLinks(){
        return links;
    }

    public void setStatus(String status){
        this.status = status;
    }

    public String getStatus(){
        return status;
    }

    @Override
    public String toString(){
        return 
            "TendersItem{" + 
            "links = '" + links + '\'' + 
            ",status = '" + status + '\'' + 
            "}";
        }
}

LinksItem.java

import com.fasterxml.jackson.annotation.JsonProperty;

public class LinksItem{

    @JsonProperty("rel")
    private String rel;

    @JsonProperty("href")
    private String href;

    public void setRel(String rel){
        this.rel = rel;
    }

    public String getRel(){
        return rel;
    }

    public void setHref(String href){
        this.href = href;
    }

    public String getHref(){
        return href;
    }

    @Override
    public String toString(){
        return 
            "LinksItem{" + 
            "rel = '" + rel + '\'' + 
            ",href = '" + href + '\'' + 
            "}";
        }
}

Invoice.java

import com.fasterxml.jackson.annotation.JsonProperty;

public class Invoice{

    @JsonProperty("new")
    private String jsonMemberNew;

    @JsonProperty("old")
    private String old;

    public void setJsonMemberNew(String jsonMemberNew){
        this.jsonMemberNew = jsonMemberNew;
    }

    public String getJsonMemberNew(){
        return jsonMemberNew;
    }

    public void setOld(String old){
        this.old = old;
    }

    public String getOld(){
        return old;
    }

    @Override
    public String toString(){
        return 
            "Invoice{" + 
            "new = '" + jsonMemberNew + '\'' + 
            ",old = '" + old + '\'' + 
            "}";
        }
}

相关问题