Java 在RESTful web服务中添加删除方法
我正在创建我的第一个RESTful web服务(使用MySQL)。但我不知道如何按id从表中删除记录 这就是我到目前为止所做的:Java 在RESTful web服务中添加删除方法,java,mysql,rest,Java,Mysql,Rest,我正在创建我的第一个RESTful web服务(使用MySQL)。但我不知道如何按id从表中删除记录 这就是我到目前为止所做的: 按id搜索人员,并以XML格式返回结果(id、全名和年龄): private PersonDao personDao = new PersonDao(); //method which return a single person in xml @GET @Path("/getPersonByIdXML/{id}") @Produces(MediaType.APPLI
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
@GET
@Path("/getPersonByIdJSON/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Person getPersonById(@PathParam("id")int id){
return personDao.getPersonById(id);
}
// the method returns list of all persons
@GET
@Path("/getAllPersonsInXML")
@Produces(MediaType.APPLICATION_XML)
public List<Person> getAllPersonsInXML(){
return personDao.getAllPersons();
}
@GET
@Path("/getPersonByIdJSON/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Person getPersonById(@PathParam("id")int id){
return personDao.getPersonById(id);
}
// the method returns list of all persons
@GET
@Path("/getAllPersonsInXML")
@Produces(MediaType.APPLICATION_XML)
public List<Person> getAllPersonsInXML(){
return personDao.getAllPersons();
}
//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\"}";
}
}
//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。应该真的是一篇文章,因为你正在创造一些东西
- 您的更新是一个获取。真的应该是一把。
玩得开心,坚持下去李>
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的方法,谢谢谢谢,我正在制作deleteById方法,但他不工作。下一个应答204中的代码不是错误,它只是表示没有返回任何内容。在这种情况下,这是意料之中的,因为删除是一个空的。检查数据库以查看记录是否已被删除。逻辑在我看来还可以,但我已经有一段时间没有完成mysql java的工作了。