gpt4 book ai didi

java - Jackson JsonMappingException 无法实例化

转载 作者:行者123 更新时间:2023-12-01 18:06:20 25 4
gpt4 key购买 nike

我正在使用 Retrofit 2 和 jackson 转换器下载并解析此 json

`{
"message": "Action complete.",
"successCode": 0,
"data": {
"miscellaneous": {
"_id": "5678efcbcf5a5dc54f401eb9",
"supportEmailDetails": {
"body": "google.com",
"subject": "google.com",
"email": "google@gmail.com"
},
"supportPhone": "12345667899",
"tempClickStaffingAgreementURL": "google.com"
}
}
}`

但它给了我以下错误:

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class modals.MMiscellaneous$Miscellaneous]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: java.io.InputStreamReader@1b44a0a3; line: 1, column: 72] (through reference chain: modals.MMiscellaneous["data"]->modals.Data["miscellaneous"])

我在 http://www.jsonschema2pojo.org/ 的帮助下生成了 pojo 并创建了一个文件,其中包含所有生成的类

但是当我在//https://timboudreau.com/blog/json/read 站点的帮助下生成 pojo 类时,它给了我正确的 jackson 结果,

我尝试过实现互联网的所有结果来解决这个问题但失败了,所以请帮助我解决这个问题

这是我的java代码,

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.HashMap;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"message",
"successCode",
"data"
})
public class MMiscellaneous {

@JsonProperty("message")
private String message;
@JsonProperty("successCode")
private Integer successCode;
@JsonProperty("data")
private Data data;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* No args constructor for use in serialization
*
*/
public MMiscellaneous() {
}

/**
*
* @param message
* @param successCode
* @param data
*/
public MMiscellaneous(String message, Integer successCode, Data data) {
this.message = message;
this.successCode = successCode;
this.data = data;
}

/**
*
* @return
* The message
*/
@JsonProperty("message")
public String getMessage() {
return message;
}

/**
*
* @param message
* The message
*/
@JsonProperty("message")
public void setMessage(String message) {
this.message = message;
}

/**
*
* @return
* The successCode
*/
@JsonProperty("successCode")
public Integer getSuccessCode() {
return successCode;
}

/**
*
* @param successCode
* The successCode
*/
@JsonProperty("successCode")
public void setSuccessCode(Integer successCode) {
this.successCode = successCode;
}

/**
*
* @return
* The data
*/
@JsonProperty("data")
public Data getData() {
return data;
}

/**
*
* @param data
* The data
*/
@JsonProperty("data")
public void setData(Data data) {
this.data = data;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"miscellaneous"
})
public class Data {

@JsonProperty("miscellaneous")
private Miscellaneous miscellaneous;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* No args constructor for use in serialization
*
*/
public Data() {
}

/**
*
* @param miscellaneous
*/
public Data(Miscellaneous miscellaneous) {
this.miscellaneous = miscellaneous;
}

/**
*
* @return
* The miscellaneous
*/
@JsonProperty("miscellaneous")
public Miscellaneous getMiscellaneous() {
return miscellaneous;
}

/**
*
* @param miscellaneous
* The miscellaneous
*/
@JsonProperty("miscellaneous")
public void setMiscellaneous(Miscellaneous miscellaneous) {
this.miscellaneous = miscellaneous;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"_id",
"supportEmailDetails",
"supportPhone",
"tempClickStaffingAgreementURL"
})
public class Miscellaneous {

@JsonProperty("_id")
private String Id;
@JsonProperty("supportEmailDetails")
private SupportEmailDetails supportEmailDetails;
@JsonProperty("supportPhone")
private String supportPhone;
@JsonProperty("tempClickStaffingAgreementURL")
private String tempClickStaffingAgreementURL;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* No args constructor for use in serialization
*
*/
public Miscellaneous() {
}

/**
*
* @param supportPhone
* @param Id
* @param tempClickStaffingAgreementURL
* @param supportEmailDetails
*/
public Miscellaneous(String Id, SupportEmailDetails supportEmailDetails, String supportPhone, String tempClickStaffingAgreementURL) {
this.Id = Id;
this.supportEmailDetails = supportEmailDetails;
this.supportPhone = supportPhone;
this.tempClickStaffingAgreementURL = tempClickStaffingAgreementURL;
}

/**
*
* @return
* The Id
*/
@JsonProperty("_id")
public String getId() {
return Id;
}

/**
*
* @param Id
* The _id
*/
@JsonProperty("_id")
public void setId(String Id) {
this.Id = Id;
}

/**
*
* @return
* The supportEmailDetails
*/
@JsonProperty("supportEmailDetails")
public SupportEmailDetails getSupportEmailDetails() {
return supportEmailDetails;
}

/**
*
* @param supportEmailDetails
* The supportEmailDetails
*/
@JsonProperty("supportEmailDetails")
public void setSupportEmailDetails(SupportEmailDetails supportEmailDetails) {
this.supportEmailDetails = supportEmailDetails;
}

/**
*
* @return
* The supportPhone
*/
@JsonProperty("supportPhone")
public String getSupportPhone() {
return supportPhone;
}

/**
*
* @param supportPhone
* The supportPhone
*/
@JsonProperty("supportPhone")
public void setSupportPhone(String supportPhone) {
this.supportPhone = supportPhone;
}

/**
*
* @return
* The tempClickStaffingAgreementURL
*/
@JsonProperty("tempClickStaffingAgreementURL")
public String getTempClickStaffingAgreementURL() {
return tempClickStaffingAgreementURL;
}

/**
*
* @param tempClickStaffingAgreementURL
* The tempClickStaffingAgreementURL
*/
@JsonProperty("tempClickStaffingAgreementURL")
public void setTempClickStaffingAgreementURL(String tempClickStaffingAgreementURL) {
this.tempClickStaffingAgreementURL = tempClickStaffingAgreementURL;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"body",
"subject",
"email"
})
public class SupportEmailDetails {

@JsonProperty("body")
private String body;
@JsonProperty("subject")
private String subject;
@JsonProperty("email")
private String email;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* No args constructor for use in serialization
*
*/
public SupportEmailDetails() {
}

/**
*
* @param body
* @param email
* @param subject
*/
public SupportEmailDetails(String body, String subject, String email) {
this.body = body;
this.subject = subject;
this.email = email;
}

/**
*
* @return
* The body
*/
@JsonProperty("body")
public String getBody() {
return body;
}

/**
*
* @param body
* The body
*/
@JsonProperty("body")
public void setBody(String body) {
this.body = body;
}

/**
*
* @return
* The subject
*/
@JsonProperty("subject")
public String getSubject() {
return subject;
}

/**
*
* @param subject
* The subject
*/
@JsonProperty("subject")
public void setSubject(String subject) {
this.subject = subject;
}

/**
*
* @return
* The email
*/
@JsonProperty("email")
public String getEmail() {
return email;
}

/**
*
* @param email
* The email
*/
@JsonProperty("email")
public void setEmail(String email) {
this.email = email;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

}

最佳答案

您需要使用公共(public)静态内部类,否则在没有外部类实例的情况下无法创建新实例。

public static class Miscellaneous {
...
}

关于java - Jackson JsonMappingException 无法实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36152061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com