我正在尝试解析具有许多多对一关系的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;
    }

}


控制器:

@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对象。因此,杰克逊不知道如何根据值Jconcorrentes构造1

10-08 01:53