我的Json看起来像以下内容
{
name: "math",
code:null,
description:"Mathematics",
id:null,
name:"math",
noExam:null,
teacher:{
id: "57869ced78aa7da0d2ed2d92",
courseGroup:"LKG",
experties:[{type: "SOCIALSTUDIES", id: "3"}, {type: "PHYSICS", id: "4"}]
},
id:"57869ced78aa7da0d2ed2d92"
}
如果您看到我的实体类,我在www.example.com中有一组枚举Teacher.java
当我尝试发布此消息时,出现错误
JsonMappingException: Can not deserialize instance of com.iris.fruits.domain.enumeration.Experties out of START_OBJECT token
我已经尝试了几乎所有的解决方案,如DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY
,但没有成功。
public class Subject implements Serializable {
// all the other fields
@JoinColumn(name = "teacher_id")
private Teacher teacher;
// getter and setter
}
public class Teacher implements Serializable {
// all the other fields
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
@Enumerated(EnumType.STRING)
@Column(name = "experties")
@JsonProperty("experties")
private List< Experties> experties;
// getter and setter
}
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum Experties implements Serializable {
MATH(1,"MATH"),
SCIENCE(2,"SCIENCE"),
SOCIALSTUDIES(3,"SOCIALSTUDIES"),
PHYSICS(4,"PHYSICS"),
CHEMISTRY(5,"CHEMISTRY");
@JsonSerialize(using = ToStringSerializer.class)
private String type;
@JsonSerialize(using = ToStringSerializer.class)
private Integer id;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
Experties(Integer id, final String type) {
this.id = id;
this.type = type;
}
}
4条答案
按热度按时间hmtdttj41#
出现此问题的原因是您的
enum
(@JsonFormat(shape = JsonFormat.Shape.OBJECT)
)中有自定义序列化程序。因此,要解决此问题,您需要自定义反序列化程序。您可以使用以下方法定义自定义反序列化程序:
自定义反序列化程序为:
当然,您可以使用其他解串器实现(例如,使用
id
字段而不是type
字段,检查id
和type
字段之间的一致性)。sshcrbum2#
你的类应该与json的结构相匹配,并且在你的输入中json不应该重复键。
我猜你的跟班,应该像下面这样:
6ljaweal3#
将JsonDeserialize注解添加到Teacher类中的 experties setter:
4smxwvx54#
您遇到这个问题是因为您在枚举
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
中有一个自定义序列化程序。Jackson不知道如何将对象从json反序列化到枚举中。您可以创建“creator”方法来轻松解决这个问题: