簡體   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

因此,我的問題是這樣的:我使用Jersey使用Java創建了RESTfull Web服務。 另外,為了映射和使用MySQL數據庫,我使用了休眠(jpa)。 我有一個名為“ horarios”的POJO。 另外,一個名為“ turmas”的POJO。 它們在Hibernate中使用@ManyToMany相互引用。 他們是這樣的:

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

@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

}

這里是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

問題是這樣的:對於每一列,對待json對象非常容易。 但是我真的不能使用多對多類型來做..這是我發送的json的示例:

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

根據http://www.jsonlint.com/有效。 但是,當我嘗試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"])

因此,我無法提取我在Turmas字段中輸入的值。

最后一個問題是 :如何(以及必須更改什么)讀取JSON文件,並將傳遞的Turmas和Horarios之間的關系放在Join列中? 就像我如何在沒有得到該錯誤的情況下讀取turmas傳遞的ID一樣?

您必須像下面那樣更改json。 根據您的horioio實體定義,您的horario應該具有turmas列表。 {“ dia”:6,“ horario”:15,“ turmas”:[{“ codigo”:“ xxxx”}]}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM