在REST风格的Web服务中添加删除方法
问题描述:
我正在制作我的第一个REST风格的Web服务(使用MySQL)。但我不知道如何通过id从表中删除记录。在REST风格的Web服务中添加删除方法
这是我迄今所做的:通过ID
-
搜索一个人,并以XML格式返回结果(ID,姓名和年龄):
private PersonDao personDao = new PersonDao(); //method which return a single person in xml @GET @Path("/getPersonByIdXML/{id}") @Produces(MediaType.APPLICATION_XML) public Person getPersonByIdXML(@PathParam("id")int id){ return personDao.getPersonById(id); } // return JSON response
-
按ID搜索某个人,并以JSON格式返回结果(id,姓名和年龄):
@GET @Path("/getPersonByIdJSON/{id}") @Produces(MediaType.APPLICATION_JSON) public Person getPersonById(@PathParam("id")int id){ return personDao.getPersonById(id); }
-
输出的所有人员和JSON格式返回结果(ID,姓名和年龄):
//insert @GET @Path("/insertPerson/{fullName}/{age}") @Produces(MediaType.APPLICATION_JSON) public String saveNewPerson(@PathParam("fullName") String fullName, @PathParam("age") int age) { Person person = new Person(); person.setFullName(fullName); person.setAge(age); if (!personDao.savePerson(person)) { return "{\"status\":\"ok\"} id="+person.getId(); } else { return "{\"status\":\"not ok\"}"; } }
-
编辑一个人在数据库:
// the method returns list of all persons @GET @Path("/getAllPersonsInXML") @Produces(MediaType.APPLICATION_XML) public List<Person> getAllPersonsInXML(){ return personDao.getAllPersons(); }
-
在数据库中插入一个人:
//update @GET @Path("/insertPerson/{id}/{fullName}/{age}") @Produces(MediaType.APPLICATION_JSON) public String updatePerson(@PathParam("id") int id, @PathParam("fullName") String fullName, @PathParam("age") int age) { Person person = new Person(); person.setId(id); person.setFullName(fullName); person.setAge(age); if (!personDao.savePerson(person)) { return "{\"status\":\"ok\"}"; } else { return "{\"status\":\"not ok\"}"; } }
答
如果你想删除资源:
@DELETE
@Path("/{id}")
public void deleteById(@PathParam("id")int id){
personDao.deleteById(id);
}
只要你有deleteById方法构造然后谢谢!
建议:
- 你插入一个GET。应该真的是一个POST,因为你正在创造一些东西。
- 您的更新是GET。应该真的是一个PUT。 玩得开心,并保持在它!
答
这种方法getPersonById:
public Person getPersonById(int id) {
Person person = null;
Session session = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
person = (Person) session.createQuery("from Person p where p.id = :ID").setParameter("ID", id).uniqueResult();
session.getTransaction().commit();
} catch (Exception ex) {
if (session != null) {
session.getTransaction().rollback();
}
} finally {
if (session != null) {
session.close();
}
}
return person;
}
这种方法删除:
public void deleteById(int id) {
Person person = null;
Session session = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
person = (Person) session.createQuery("delete Person p where p.id = :ID");
session.getTransaction().commit();
} catch (Exception ex) {
if (session != null) {
session.getTransaction().rollback();
}
} finally {
if (session != null) {
session.close();
}
}
}
deleteById无法正常工作,输出误差204没有内容,如何为deleteById做正确的方法,谢谢
+0
204不是错误,它只是表示没有返回内容。在这种情况下,这是预期的,因为DELETE是无效的。检查您的数据库以查看记录是否已被删除。逻辑对我来说看起来不错,但我在一段时间内没有完成我的mysql java工作。 – 2015-04-02 16:21:10
谢谢,我使deleteById方法,但他不工作。下一个答案中的代码 – Lev 2015-04-02 09:46:50