Java 在Spring MVC上使用jackson和相关实体解析JSON
我正在尝试解析一个具有几个多对一关系的jcelulas对象。我似乎无法正确解析它们。感谢您的帮助 型号:Java 在Spring MVC上使用jackson和相关实体解析JSON,java,json,spring,spring-mvc,jackson,Java,Json,Spring,Spring Mvc,Jackson,我正在尝试解析一个具有几个多对一关系的jcelulas对象。我似乎无法正确解析它们。感谢您的帮助 型号: @Entity @Table(name = "jcelulas", catalog = "7jogos") public class Jcelulas implements java.io.Serializable { private Integer id; private Jconcorrentes jconcorrentes; private Jgrelhas
@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;
}
}
控制器:
@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);
}
@实体
@表(name=“jconcorrentes”,catalog=“7jogos”)
公共类Jconcorrentes实现java.io.Serializable{
私有整数id;
....
私人日期数据登记处;
私有集jcelulases=新哈希集(0);
}
PS:这些模型是用hibernate从mysql数据库生成的问题是,
jconcorrentes
被序列化为一个数字,而不是JSON对象。所以Jackson不知道如何构造一个Jconcorrentes
值外1
你能分享你的Jconcorrentes
类吗?问题似乎在于它。添加了jconcorrentes模型。但这些模型是由Hibernate生成的。