简体   繁体   English

在RESTful Web服务中添加删除方法

[英]Add Delete Method in a RESTful web service

I am making my first RESTful web service (using MySQL). 我正在制作我的第一个RESTful Web服务(使用MySQL)。 But I don't know how to remove records from a table by id. 但是我不知道如何通过ID从表中删除记录。

This is what I have done so far: 到目前为止,这是我所做的:

  1. Search for a person by id and return the result(id, full name and age) in XML format: 通过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 
  2. Search for a person by id and return the result(id, full name and age) in JSON format: 通过id搜索人并以JSON格式返回结果(id,全名和年龄):

     @GET @Path("/getPersonByIdJSON/{id}") @Produces(MediaType.APPLICATION_JSON) public Person getPersonById(@PathParam("id")int id){ return personDao.getPersonById(id); } 
  3. Output all persons and return the result(id, full name and age) in JSON format: 输出所有人并以JSON格式返回结果(id,全名和年龄):

     // the method returns list of all persons @GET @Path("/getAllPersonsInXML") @Produces(MediaType.APPLICATION_XML) public List<Person> getAllPersonsInXML(){ return personDao.getAllPersons(); } 
  4. Insert a person in the database: 在数据库中插入一个人:

     //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\\"}"; } } 
  5. Edit a person in the database: 在数据库中编辑一个人:

     //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\\"}"; } } 

If you want a DELETE resource: 如果要删除资源:

@DELETE
@Path("/{id}")
public void deleteById(@PathParam("id")int id){
   personDao.deleteById(id);
}

As long as you have the deleteById method constructed then Thanks it! 只要您构造了deleteById方法,那就谢谢!

Suggestions: 建议:

  • Your insert is a GET. 您的插入内容是GET。 Should really be a POST since you are creating something. 由于您正在创建某些内容,因此应该真正是POST。
  • Your update is a GET. 您的更新是GET。 Should really be a PUT. 应该真的是一个PUT。 Have fun and keep at it!! 玩得开心并坚持下去!!

this method for getPersonById: 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;
}

this method for Delete: 此删除方法:

    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 not work, output error 204 No Content, how do correct method for deleteById, thanks deleteById不起作用,输出错误204否内容,如何为deleteById设置正确的方法,谢谢

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

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