使用 Jackson Parser 时的默认构造函数问题
Default Constructor issue while using Jackson Parser
我的模型 getter setter class 看起来像这样:-
@JsonIgnoreProperties(ignoreUnknown = true)
public class CuratedOffers {
public CuratedOffers() {
}
@JsonProperty("response")
private String response;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@JsonProperty("data")
private Data mData;
public Data getmData() {
return mData;
}
public void setmData(Data mData) {
this.mData = mData;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Data{
@JsonProperty("vendors")
private List<Vendor> vendorList;
public List<Vendor> getVendorList() {
return vendorList;
}
public void setVendorList(List<Vendor> vendorList) {
this.vendorList = vendorList;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Vendor {
@JsonProperty("id")
private String Id;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
@JsonProperty("name")
private String venName;
public String getVenName() {
return venName;
}
public void setVenName(String venName) {
this.venName = venName;
}
@JsonProperty("image")
private String image;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@JsonProperty("logo")
private String logo;
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
@JsonProperty("offers")
private String offers;
public String getOffers() {
return offers;
}
public void setOffers(String offers) {
this.offers = offers;
}
@JsonProperty("description")
private String offer_description;
public void setOffer_description(String offer_description) {
this.offer_description = offer_description;
}
public String getOffer_description() {
return offer_description;
}
}
}
我在通过 gradle 进行编译时使用 Jackson,即:-
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.2'
compile 'com.fasterxml.jackson.core:jackson-core:2.4.2'
编译后,我的堆栈跟踪中一直出现此错误
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.yoy.CuratedOffers$Data]: can not instantiate from JSON object (need to add/enable type information?)
搜索不到任何合适的 SO answers.Help 将不胜感激!!
根据@vilpel89 的建议,我忘记在 CuratedOffers class.Now 中声明静态嵌套 class 我更新的 class 是:-
@JsonIgnoreProperties(ignoreUnknown = true)
public class CuratedOffers {
public CuratedOffers() {
}
@JsonProperty("response")
private String response;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@JsonProperty("data")
private Data mData;
public Data getmData() {
return mData;
}
public void setmData(Data mData) {
this.mData = mData;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Data{
public Data() {
}
@JsonProperty("vendors")
private List<Vendor> vendorList;
public List<Vendor> getVendorList() {
return vendorList;
}
public void setVendorList(List<Vendor> vendorList) {
this.vendorList = vendorList;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Vendor {
public Vendor() {
}
@JsonProperty("id")
private String Id;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
@JsonProperty("name")
private String venName;
public String getVenName() {
return venName;
}
public void setVenName(String venName) {
this.venName = venName;
}
@JsonProperty("image")
private String image;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@JsonProperty("logo")
private String logo;
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
@JsonProperty("offers")
private String offers;
public String getOffers() {
return offers;
}
public void setOffers(String offers) {
this.offers = offers;
}
@JsonProperty("description")
private String offer_description;
public void setOffer_description(String offer_description) {
this.offer_description = offer_description;
}
public String getOffer_description() {
return offer_description;
}
}
}
向我的数据 class 和供应商 Class 添加了一个静态构造函数,并且还向 it.Now 添加了默认构造函数,它的工作就像一个魅力!希望有一天它可能对某人有所帮助!
我的模型 getter setter class 看起来像这样:-
@JsonIgnoreProperties(ignoreUnknown = true)
public class CuratedOffers {
public CuratedOffers() {
}
@JsonProperty("response")
private String response;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@JsonProperty("data")
private Data mData;
public Data getmData() {
return mData;
}
public void setmData(Data mData) {
this.mData = mData;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Data{
@JsonProperty("vendors")
private List<Vendor> vendorList;
public List<Vendor> getVendorList() {
return vendorList;
}
public void setVendorList(List<Vendor> vendorList) {
this.vendorList = vendorList;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Vendor {
@JsonProperty("id")
private String Id;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
@JsonProperty("name")
private String venName;
public String getVenName() {
return venName;
}
public void setVenName(String venName) {
this.venName = venName;
}
@JsonProperty("image")
private String image;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@JsonProperty("logo")
private String logo;
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
@JsonProperty("offers")
private String offers;
public String getOffers() {
return offers;
}
public void setOffers(String offers) {
this.offers = offers;
}
@JsonProperty("description")
private String offer_description;
public void setOffer_description(String offer_description) {
this.offer_description = offer_description;
}
public String getOffer_description() {
return offer_description;
}
}
}
我在通过 gradle 进行编译时使用 Jackson,即:-
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.2'
compile 'com.fasterxml.jackson.core:jackson-core:2.4.2'
编译后,我的堆栈跟踪中一直出现此错误
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.yoy.CuratedOffers$Data]: can not instantiate from JSON object (need to add/enable type information?)
搜索不到任何合适的 SO answers.Help 将不胜感激!!
根据@vilpel89 的建议,我忘记在 CuratedOffers class.Now 中声明静态嵌套 class 我更新的 class 是:-
@JsonIgnoreProperties(ignoreUnknown = true)
public class CuratedOffers {
public CuratedOffers() {
}
@JsonProperty("response")
private String response;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@JsonProperty("data")
private Data mData;
public Data getmData() {
return mData;
}
public void setmData(Data mData) {
this.mData = mData;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Data{
public Data() {
}
@JsonProperty("vendors")
private List<Vendor> vendorList;
public List<Vendor> getVendorList() {
return vendorList;
}
public void setVendorList(List<Vendor> vendorList) {
this.vendorList = vendorList;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Vendor {
public Vendor() {
}
@JsonProperty("id")
private String Id;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
@JsonProperty("name")
private String venName;
public String getVenName() {
return venName;
}
public void setVenName(String venName) {
this.venName = venName;
}
@JsonProperty("image")
private String image;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@JsonProperty("logo")
private String logo;
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
@JsonProperty("offers")
private String offers;
public String getOffers() {
return offers;
}
public void setOffers(String offers) {
this.offers = offers;
}
@JsonProperty("description")
private String offer_description;
public void setOffer_description(String offer_description) {
this.offer_description = offer_description;
}
public String getOffer_description() {
return offer_description;
}
}
}
向我的数据 class 和供应商 Class 添加了一个静态构造函数,并且还向 it.Now 添加了默认构造函数,它的工作就像一个魅力!希望有一天它可能对某人有所帮助!