gpt4 book ai didi

java - 在 Spring MVC 上使用 jackson 和相关实体解析 JSON

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:12 27 4
gpt4 key购买 nike

我正在尝试解析具有一些多对一关系的 jcelulas 对象。我似乎无法正确解析它们。如有任何帮助,我们将不胜感激。

型号:

@Entity
@Table(name = "jcelulas", catalog = "7jogos")
public class Jcelulas implements java.io.Serializable {

private Integer id;
private Jconcorrentes jconcorrentes;
private Jgrelhas jgrelhas;
private Jcodigos jcodigos;
private Jpremios jpremios;
private boolean checked;
private Date dataChecked;

// getters and setters

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ConcorrentesId")
public Jconcorrentes getJconcorrentes() {
return this.jconcorrentes;
}

public void setJconcorrentes(Jconcorrentes jconcorrentes) {
this.jconcorrentes = jconcorrentes;
}

}

Controller :

@RequestMapping(value = "/jtabuleiros/play/commit", 
method = RequestMethod.POST,
headers = {"Content-type=application/json"})
@ResponseBody
public JsonResponse playcelula(@ModelAttribute DataJson celula,@RequestBody String json) {
System.out.println(celula.toString());
System.out.println(json);

ObjectMapper mapper = new ObjectMapper();

try {

// read from file, convert it to user class
Jcelulas user = mapper.readValue(json, Jcelulas.class);

// display to console
System.out.println(user);

} catch (JsonGenerationException e) {

e.printStackTrace();

} catch (JsonMappingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return new JsonResponse("OK","");
}

请求:

{
"id":1,
"jconcorrentes":1,
"jgrelhas":1,
"jcodigos":1
}

我应该如何解析jconcorrentes?我尝试使用 int 并得到以下错误:

org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class com.setelog.spring.model.Jconcorrentes] from JSON integral number; no single-int-arg constructor/factory method (through reference chain: com.setelog.spring.model.Jcelulas["jconcorrentes"])

Jconcorrentes:

@Entity
@Table(name = "jconcorrentes", catalog = "7jogos")
public class Jconcorrentes implements java.io.Serializable {

private Integer id;

....
private Date dataRegisto;
private Set<Jcelulas> jcelulases = new HashSet<Jcelulas>(0);

}

PS:这些模型是使用 hibernate 从 mysql 数据库生成的

最佳答案

问题是 jconcorrentes 被序列化为数字,而不是 JSON 对象。因此 Jackson 不知道如何构造一个值 1Jconcorrentes

关于java - 在 Spring MVC 上使用 jackson 和相关实体解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29899320/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com