2015-04-01 125 views
0

我正在製作我的第一個REST風格的Web服務(使用MySQL)。但我不知道如何通過id從表中刪除記錄。在REST風格的Web服務中添加刪除方法

這是我迄今所做的:通過ID

  1. 搜索一個人,並以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,姓名和年齡):

    //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\"}"; 
        } 
    } 
    
  4. 編輯一個人在數據庫:

    // the method returns list of all persons 
    @GET 
    @Path("/getAllPersonsInXML") 
    @Produces(MediaType.APPLICATION_XML) 
    public List<Person> getAllPersonsInXML(){ 
        return personDao.getAllPersons(); 
    } 
    
  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\"}"; 
        }  
    } 
    

回答

2

如果你想刪除資源:

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

只要你有deleteById方法構造然後謝謝!

建議:

  • 你插入一個GET。應該真的是一個POST,因爲你正在創造一些東西。
  • 您的更新是GET。應該真的是一個PUT。 玩得開心,並保持在它!
+0

謝謝,我使deleteById方法,但他不工作。下一個答案中的代碼 – Lev 2015-04-02 09:46:50

0

這種方法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