我想用Jersey和Hibernate構建一個簡單的REST API。我使用的數據庫是Neo4j。休眠OGM簡單REST API
每當我嘗試做一個簡單的GET請求,我得到500錯誤,我不知道我做錯了什麼。
我的項目結構是這樣的:
這裏是我的Person類:
package com.topjavatutorial.dao;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Person")
public class Person {
@Id @GeneratedValue
@Column(name="id")
private int id;
@Column(name="gender")
private String gender;
@Column(name="dob")
private String dob;
@Column(name="firstname")
private String firstname;
@Column(name="middlename")
private String middlename;
@Column(name="lastname")
private String lastname;
// getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getMiddlename() {
return middlename;
}
public void setMiddlename(String middlename) {
this.middlename = middlename;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
我的PersonDAO類看起來是這樣的:
package com.topjavatutorial.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.hibernate.HibernateException;
public class PersonDAO {
public Person getPerson(Integer id){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Person person = new Person();
try{
String query = "MATCH (p:Person {id:" + id + "}) "
+ "RETURN {id: p.id, "
+ "firstname: p.firstname, "
+ "middlename: p.middlename, "
+ "lastname: p.lastname, "
+ "dob: p.dob, "
+ "gender: p.gender}";
person = (Person) em.createNativeQuery(query).getResultList();
em.flush();
tx.commit();
em.clear();
em.close();
emf.close();
}
catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}
return person;
}
}
和MyResource類是這樣的:
package com.topjavatutorial;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.topjavatutorial.dao.Person;
import com.topjavatutorial.dao.PersonDAO;
@Path("/people")
public class MyResource {
@GET
@Produces("application/json")
public Person getPeople() {
PersonDAO person = new PersonDAO();
return person.getPerson(6);
}
}
所以,當我運行以下網址的應用:http://localhost:8080/Neo4jAPI/
,我沒有得到任何錯誤,但每當我做http://localhost:8080/Neo4jAPI/webapi/people
,我得到的500錯誤。
謝謝你的回覆。我相信該網址應該是:'http:// localhost:8080/Neo4jAPI/webapi/people',並與該網址我得到500錯誤:'javax.persistence.PersistenceException:[PersistenceUnit:持久性]無法建立Hibernate SessionFactory' 。我已經改變了你說的代碼。我現在用新代碼更新我的問題 – Porjaz
當服務器端出現故障時,500內部服務器錯誤是默認的,所以我強烈懷疑你有拋出的異常,堆棧跟蹤將幫助你找到答案。 – MartinByers