【问题标题】:Add Delete Method in a RESTful web service在 RESTful Web 服务中添加删除方法
【发布时间】:2015-06-06 18:09:29
【问题描述】:

我正在制作我的第一个 RESTful Web 服务(使用 MySQL)。但我不知道如何通过 id 从表中删除记录。

这是我到目前为止所做的:

  1. 通过 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. 通过 id 搜索一个人,并以 JSON 格式返回结果(id、全名和年龄):

    @GET
    @Path("/getPersonByIdJSON/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Person getPersonById(@PathParam("id")int id){
        return personDao.getPersonById(id);
    }
    
  3. 输出所有人并以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
    @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. 在数据库中编辑一个人:

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

【问题讨论】:

    标签: java mysql rest


    【解决方案1】:

    如果你想要一个 DELETE 资源:

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

    只要您构造了 deleteById 方法,那么谢谢!

    建议:

    • 您的插入是一个 GET。真的应该是 POST,因为您正在创建一些东西。
    • 您的更新是 GET。真的应该是一个PUT。 玩得开心并坚持下去!

    【讨论】:

    • 谢谢,我正在使用 deleteById 方法,但他不起作用。下面的代码在下一个答案中
    【解决方案2】:

    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 No Content,deleteById 的正确方法,谢谢

    【讨论】:

    • 204 不是错误,它只是意味着没有返回内容。在这种情况下这是预期的,因为 DELETE 是无效的。检查您的数据库以查看记录是否已被删除。逻辑对我来说看起来不错,但我有一段时间没有完成我的 mysql java 工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    相关资源
    最近更新 更多