我有一个如下所示的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
2条答案
按热度按时间rjjhvcjd1#
无法构造
com.teamx.InvoicePayment$Tender
的示例:像这样的非静态内部类只能使用默认的无参数构造函数进行示例化这意味着Jackson不能示例化
Invoice
类,因为作为非静态的结果,它需要一个封闭类的示例(而构建InvoicePayment
又需要Invoice
的示例)。因此,您需要将
static
修饰符应用于Invoice
类,同样的方法也适用于Tender
类。2g32fytz2#
Maven依赖
第一个月
TendersItem.java
LinksItem.java
Invoice.java