JsonMappingException:找不到类型 [简单类型,class car.Car$Parts] 的合适构造函数
JsonMappingException: No suitable constructor found for type [simple type, class car.Car$Parts]
我正在尝试将此 XML 反序列化为 Parts
对象:
<Parts>
<Part>
<Name>gearbox</Name>
<Year>1990</Year>
</Part>
<Part>
<Name>wheel</Name>
<Year>2000</Year>
</Part>
</Parts>
Car.java:
package problem.car;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Car {
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Parts"
})
public class Parts {
@JsonProperty("parts")
private List<Part> parts = new ArrayList<>();
@JsonProperty("parts")
public List<Part> getParts() {
return parts;
}
@JsonProperty("parts")
public void setParts(List<Part> parts) {
this.parts = parts;
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name",
"Year"
})
public class Part {
@JsonProperty("Name")
private String Name;
@JsonProperty("Year")
private String Year;
@JsonProperty("Name")
public String getName() {
return Name;
}
@JsonProperty("Name")
public void setName(String Name) {
this.Name = Name;
}
@JsonProperty("Year")
public String getYear() {
return Year;
}
@JsonProperty("Year")
public void setYear(String Year) {
this.Year = Year;
}
}
public static void main(String args[]) {
try {
String xml = "<Parts>\n"
+ " <Part>\n"
+ " <Name>gearbox</Name>\n"
+ " <Year>1990</Year>\n"
+ " </Part>\n"
+ " <Part>\n"
+ " <Name>wheel</Name>\n"
+ " <Year>2000</Year>\n"
+ " </Part>\n"
+ "</Parts>";
Parts parts = (Parts) deserialize(Parts.class, xml);
} catch (IOException ex) {
Logger.getLogger(Car.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static final Object deserialize(final Class clazz, final String xml) throws IOException {
ObjectMapper xmlMapper = new XmlMapper();
xmlMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"));
Object object;
try {
object = xmlMapper.readValue(xml, clazz);
} catch (com.fasterxml.jackson.databind.exc.InvalidFormatException ex) {
xmlMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
object = xmlMapper.readValue(xml, clazz);
}
return object;
}
}
虽然我没有发现我的代码有任何问题,但为什么它一直给我以下信息?
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class problem.car.Car$Parts]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
答案
Part
class 是一个嵌套的、非静态的 class。
为了实例化它,你必须有一个外部 class.
的实例
Here is an inner class tutorial
我讨厌阅读答案
将零件和零件 classes 移动到它们自己的 java 文件中,命名为 Part.java 和 Parts.java。
尝试用大写字母书写,就像在 class 正文中一样:
@XmlType(name = "Parts", propOrder = {
"Part"
})
因为jackson反序列化是区分大小写的。
我正在尝试将此 XML 反序列化为 Parts
对象:
<Parts>
<Part>
<Name>gearbox</Name>
<Year>1990</Year>
</Part>
<Part>
<Name>wheel</Name>
<Year>2000</Year>
</Part>
</Parts>
Car.java:
package problem.car;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Car {
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Parts"
})
public class Parts {
@JsonProperty("parts")
private List<Part> parts = new ArrayList<>();
@JsonProperty("parts")
public List<Part> getParts() {
return parts;
}
@JsonProperty("parts")
public void setParts(List<Part> parts) {
this.parts = parts;
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name",
"Year"
})
public class Part {
@JsonProperty("Name")
private String Name;
@JsonProperty("Year")
private String Year;
@JsonProperty("Name")
public String getName() {
return Name;
}
@JsonProperty("Name")
public void setName(String Name) {
this.Name = Name;
}
@JsonProperty("Year")
public String getYear() {
return Year;
}
@JsonProperty("Year")
public void setYear(String Year) {
this.Year = Year;
}
}
public static void main(String args[]) {
try {
String xml = "<Parts>\n"
+ " <Part>\n"
+ " <Name>gearbox</Name>\n"
+ " <Year>1990</Year>\n"
+ " </Part>\n"
+ " <Part>\n"
+ " <Name>wheel</Name>\n"
+ " <Year>2000</Year>\n"
+ " </Part>\n"
+ "</Parts>";
Parts parts = (Parts) deserialize(Parts.class, xml);
} catch (IOException ex) {
Logger.getLogger(Car.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static final Object deserialize(final Class clazz, final String xml) throws IOException {
ObjectMapper xmlMapper = new XmlMapper();
xmlMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"));
Object object;
try {
object = xmlMapper.readValue(xml, clazz);
} catch (com.fasterxml.jackson.databind.exc.InvalidFormatException ex) {
xmlMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
object = xmlMapper.readValue(xml, clazz);
}
return object;
}
}
虽然我没有发现我的代码有任何问题,但为什么它一直给我以下信息?
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class problem.car.Car$Parts]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
答案
Part
class 是一个嵌套的、非静态的 class。
为了实例化它,你必须有一个外部 class.
Here is an inner class tutorial
我讨厌阅读答案
将零件和零件 classes 移动到它们自己的 java 文件中,命名为 Part.java 和 Parts.java。
尝试用大写字母书写,就像在 class 正文中一样:
@XmlType(name = "Parts", propOrder = {
"Part"
})
因为jackson反序列化是区分大小写的。