Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc JsonMappingException:无法反序列化启动\u对象令牌之外的枚举实例_Spring Mvc_Enums_Jackson_Set - Fatal编程技术网

Spring mvc JsonMappingException:无法反序列化启动\u对象令牌之外的枚举实例

Spring mvc JsonMappingException:无法反序列化启动\u对象令牌之外的枚举实例,spring-mvc,enums,jackson,set,Spring Mvc,Enums,Jackson,Set,我的Json如下所示 { 名称:“数学”, 代码:空, 描述:“数学”, id:null, 名称:“数学”, noExam:null, 老师:{ id:“57869ced78aa7da0d2ed2d92”, 课程组:“LKG”, 专家:[{类型:“社会研究”,id:“3”},{类型:“物理学”,id:“4”}] }, id:“57869ced78aa7da0d2ed2d92” }您的类应该与json的结构相匹配。在您的输入中,json不应该重复键 我想你们班,应该是这样的: public cl

我的Json如下所示

{
名称:“数学”,
代码:空,
描述:“数学”,
id:null,
名称:“数学”,
noExam:null,
老师:{
id:“57869ced78aa7da0d2ed2d92”,
课程组:“LKG”,
专家:[{类型:“社会研究”,id:“3”},{类型:“物理学”,id:“4”}]
},
id:“57869ced78aa7da0d2ed2d92”

}
您的类应该与json的结构相匹配。在您的输入中,json不应该重复键

我想你们班,应该是这样的:

public class Subject implements Serializable {
// all the other fields 
    String name;
    String code;
    String description;
    String id;
    String noExam;
    @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;

    String courseGroup;
  // 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; 
    }


}
公共类主题实现可序列化{
//所有其他领域
字符串名;
字符串代码;
字符串描述;
字符串id;
字符串noExam;
@JoinColumn(name=“教师id”)
私人教师;
//接二连三
}
公共课堂教师实现可序列化{
//所有其他领域
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有字符串id;
@枚举(EnumType.STRING)
@列(名称=“专家”)
@JsonProperty(“专家”)
私人列表<专家>专家;
弦乐组;
//接二连三
}
@JsonFormat(shape=JsonFormat.shape.OBJECT)
公共枚举专家实现可序列化{
数学(1,“数学”),
科学(2,“科学”),
社会研究(3,“社会研究”),
物理学(4,“物理学”),
化学(5,“化学”);
@JsonSerialize(使用=ToStringSerializer.class)
私有字符串类型;
@JsonSerialize(使用=ToStringSerializer.class)
私有整数id;
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
专家(整数id,最终字符串类型){
this.id=id;
this.type=type;
}
}

添加JsonDeserialize教师课堂专家设置注释:

@JsonDeserialize(using = EnumDeserializer.class)
public void setExperties(List experties){
//...
}

出现此问题是因为在
枚举中有一个自定义序列化程序(
@JsonFormat(shape=JsonFormat.shape.OBJECT)
)。因此,要解决这个问题,您需要一个自定义反序列化程序

您可以使用以下命令定义自定义反序列化程序:

@JsonFormat(shape = JsonFormat.Shape.OBJECT) // custom serializer
@JsonDeserialize(using = MyEnumDeserializer.class) // custom deserializer
public enum Experties implements Serializable {
    ...
}
自定义反序列化程序为:

public static class MyEnumDeserializer extends JsonDeserializer<Experties> {
    @Override
    public Experties deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        String type = node.get("type").asText();
        return Stream.of(Experties.values())
           .filter(enumValue -> enumValue.getType().equals(type))
           .findFirst()
           .orElseThrow(() -> new IllegalArgumentException("type "+type+" is not recognized"));
    }
}
公共静态类MyEnumDeserializer扩展JsonDeserializer{
@凌驾
公共专家反序列化(JsonParser、JsonParser、反序列化上下文、反序列化上下文)抛出IOException、JsonProcessingException{
JsonNode节点=jsonParser.getCodec().readTree(jsonParser);
字符串类型=node.get(“type”).asText();
返回Stream.of(Experties.values())
.filter(enumValue->enumValue.getType().equals(类型))
.findFirst()
.orelsetrow(()->新的IllegalArgumentException(“类型“+type+”不可识别”);
}
}

当然,您可以使用另一个反序列化器实现(例如,使用
id
字段而不是
type
字段,检查
id
type
字段之间的一致性)。

我在类中有所有这些字段,只是没有将它写在这里用于空间约束。请尝试以下操作:ObjectMapper mapper=new ObjectMapper();enable(反序列化功能。接受单个值作为数组);ResultOb=mapper.readValue(jsonInput,ResultOb.class);您确定需要
Teacher.experties
上的
@Enumerated(EnumType.STRING)