您好我正在尝试将 JSON 数据转换为类,我使用这样的反序列化方法:
public class GroupDeserlialize extends StdDeserializer<Employe> {
public GroupDeserlialize()
{
this(null);
}
public GroupDeserlialize(Class<?> vc)
{
super(vc);
}
@Override
public Employe deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
// TODO Auto-generated method stub
JsonNode node = p.getCodec().readTree(p);
int matricule = (Integer) node.get("matricule").intValue();
String nom= (String) node.get("nom").textValue();
String prenom= (String) node.get("prenom").textValue();
String nom_ar= (String) node.get("nom_ar").textValue();
String prenom_ar= (String) node.get("prenom_ar").textValue();
int age = (Integer) node.get("age").numberValue();
String description= (String) node.get("description").textValue();
String email= (String) node.get("email").textValue();
String adresse= (String) node.get("adresse").textValue();
String ville= (String) node.get("ville").textValue();
int codeP = (Integer) node.get("codeP").intValue();
int numTele = (Integer) node.get("numTele").intValue();
// Long idGrp = (Long) ((LongNode) node.get("idGrp")).numberValue();
Long idGrp = (Long) node.get(0).numberValue();
String nomGroup = (String) node.get("nomGroup").asText();
return new Employe( matricule, nom, prenom, nom_ar, prenom_ar, age, description, email, codeP, numTele, adresse, ville, new Group(idGrp,nomGroup));
}}
我要发送的 JSON(如果 JSON 格式错误,这就是 Spring Boot IDK 的角度返回/n
):
{
"matricule" : "125",
"nom" : "Assaibi",
"prenom" : "Takwa",
"nom_ar" : "Assaibi",
"prenom_ar" : "Takwa",
"age" : 34,
"description" : "kjjhhdh",
"codeP" : "2087",
"email" : "takwaassaibi08@gmail.com",
"numTele" : "51890562",
"adresse" : "4270 citè folla agba Tunis",
"ville" : "Tunis",
"group" : "{\n \"nomGroup\": \"groupeMembre\",\n \"idGrp\": 3\n}"
}
这是包含一个组的类雇员
@Entity
@JsonDeserialize(using = GroupDeserlialize.class)
public class Employe implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "myid")
@GenericGenerator(name = "myid", strategy = "entity.MyGenerator")
private Integer matricule;
private String nom;
private String prenom;
private String nom_ar;
private String prenom_ar;
private int age;
private String description;
private String email;
private int codeP;
private int numTele;
private String adresse;
private String ville;
@JoinColumn(name="idGrp")
@ManyToOne(cascade = {CascadeType.ALL})
@Embedded
Group group;
public Employe(Integer matricule, String nom, String prenom, String nom_ar, String prenom_ar, int age,
String description, String email, int codeP, int numTele, String adresse, String ville, Group group) {
super();
this.matricule = matricule;
this.nom = nom;
this.prenom = prenom;
this.nom_ar = nom_ar;
this.prenom_ar = prenom_ar;
this.age = age;
this.description = description;
this.email = email;
this.codeP = codeP;
this.numTele = numTele;
this.adresse = adresse;
this.ville = ville;
}
}
这是 Group 类
@Entity
@Table(name="groups")
public class Group implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long idGrp;
private String NomGroup;
public Group(){
}
public Group(Long id, String nomGroup) {
super();
this.idGrp = id;
NomGroup = nomGroup;
}
}
和主班
public static void main(String[] args) throws JsonProcessingException {
SpringApplication.run(CniAppApplication.class, args);
Employe s=new Employe();
ObjectMapper mapper = new ObjectMapper();
/*SimpleModule module = new SimpleModule();
module.addDeserializer(Employe.class, new GroupDeserlialize());
mapper.registerModule(module);
try {
Employe readValue = mapper.readValue(mapper.writeValueAsString(s), Employe.class);
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
Employe itemWithOwner = new ObjectMapper().readValue(mapper.writeValueAsString(s), Employe.class);
}
}
我得到的例外:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.fasterxml.jackson.databind.JsonNode.numberValue()" because the return value of "com.fasterxml.jackson.databind.JsonNode.get(int)" is null
at entity.GroupDeserlialize.deserialize(GroupDeserlialize.java:48)
at entity.GroupDeserlialize.deserialize(GroupDeserlialize.java:1)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3434)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3402)
at cni.tn.CniApp.CniAppApplication.main(CniAppApplication.java:57)