0
的json看起來像下面JsonMappingException:無法反序列化枚舉的例子出來START_OBJECT令牌
{
name: "math",
code:null,
description:"Mathematics",
id:null,
name:"math",
noExam:null,
teacher:{
\t id: "57869ced78aa7da0d2ed2d92",
\t courseGroup:"LKG",
\t experties:[{type: "SOCIALSTUDIES", id: "3"}, {type: "PHYSICS", id: "4"}]
\t },
id:"57869ced78aa7da0d2ed2d92"
}
,如果你看到我的實體類,我在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");
\t
\t @JsonSerialize(using = ToStringSerializer.class)
\t private String type;
\t
\t @JsonSerialize(using = ToStringSerializer.class)
\t private Integer id;
\t
\t public String getType() {
\t \t return type;
\t }
\t public void setType(String type) {
\t \t this.type = type;
\t }
\t public Integer getId() {
\t \t return id;
\t }
\t public void setId(Integer id) {
\t \t this.id = id;
\t }
\t
\t
\t Experties(Integer id, final String type) {
\t \t this.id = id; \t \t
\t \t this.type = type;
\t }
\t
\t
}
我已經在類中的所有這些領域,只是沒有寫在這裏的空間限制。 – Mayurb
試試這個:ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); ResultOb ob = mapper.readValue(jsonInput,ResultOb.class); – rsh