简体   繁体   English

CRUD在休眠状态下从ManyToMany中使用Jersey使用Java的REST API在REST API中使用POST方法读取JSON的问题

[英]CRUD issue reading a JSON using POST method in a REST API in Java using Jersey from a ManyToMany noted in hibernate

So, my problem goes like this: I created a RESTfull web service in Java using Jersey. 因此,我的问题是这样的:我使用Jersey使用Java创建了RESTfull Web服务。 Also, to map and use the MySQL database, I used hibernate (jpa). 另外,为了映射和使用MySQL数据库,我使用了休眠(jpa)。 I have a POJO named "horarios". 我有一个名为“ horarios”的POJO。 Also, a POJO named "turmas". 另外,一个名为“ turmas”的POJO。 They are refered to each other using a @ManyToMany notaion in Hibernate. 它们在Hibernate中使用@ManyToMany相互引用。 They go as this: 他们是这样的:

Turmas.java Turmas.java

@Entity
public class Turmas {

@Id  
@GeneratedValue
@Column(name = "turmas_id", unique = true, nullable = false)
int Id;
@Column
String codigo;

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "turmas_horarios", joinColumns = {
        @JoinColumn(name = "turmas_id", nullable = false, updatable = false)     },
        inverseJoinColumns = { @JoinColumn(name = "horarios_id",nullable =     false, updatable = false) })
private List<horarios> Horarios;
}
//... getters and setters

Horarios.java Horarios.java

@Entity
public class horarios {

@Id
@GeneratedValue
@Column(name = "horarios_id", nullable = false, unique = true)
int Id;

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "Horarios")
private List<Turmas> turmas;

@Column
private int horario;

@Column
private int dia;
//...getters and setters

}

And here goes the HorariosController: 这里是HorariosController:

@Path("/horarios")
public class HorariosController {

@Path("/setHorarios")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response setHorarios(horarios h) throws Exception
{
    horarios h1 = new horarios();
    h1.setDia(h.getDia());
    h1.setHorario(h.getHorario());
    h1.setTurmas(h.getTurmas());
    Querys.Persist(h1);
    return Response.status(200).build();
}

}   

//...GET and DELETE methods

The problem goes like this: For every column, it is very easy to treat the json object. 问题是这样的:对于每一列,对待json对象非常容易。 But I really can't do it using a many to many type.. here goes an example of a json I'm sending: 但是我真的不能使用多对多类型来做..这是我发送的json的示例:

{"dia": 6,"horario": 15,"turmas": {"codigo":"xxxx"}}

Which is valid according to http://www.jsonlint.com/ . 根据http://www.jsonlint.com/有效。 Though, when I try the POST method, it gives me the return: 但是,当我尝试POST方法时,它会给我返回:

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@1b23d8c; line: 3, column: 15] (through reference chain: modules.horarios["turmas"])

And for that reason I can't extract the value I put in the Turmas' fields. 因此,我无法提取我在Turmas字段中输入的值。

The final question is : How (and what do I have to change) to read the JSON file and put in the Join Column the relationship between a passed Turmas and Horarios? 最后一个问题是 :如何(以及必须更改什么)读取JSON文件,并将传递的Turmas和Horarios之间的关系放在Join列中? Like how can I read the turmas' passed Id (for example) without getting that error? 就像我如何在没有得到该错误的情况下读取turmas传递的ID一样?

you have to change your json like below. 您必须像下面那样更改json。 As per your horaio entity definition your horario should have list of turmas. 根据您的horioio实体定义,您的horario应该具有turmas列表。 {"dia": 6,"horario": 15,"turmas": [{"codigo":"xxxx"}]} {“ dia”:6,“ horario”:15,“ turmas”:[{“ codigo”:“ xxxx”}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM