2015-09-04 51 views
0

我正在構建一個簡單的web應用程序,在java中使用rest服務。我想更新Person類型的對象列表。這是我到目前爲止的代碼:未使用java web服務更新的對象列表

public class Provider { 

    private final PersonList pl = new PersonList(); 

    public Provider() { 
     final List<Person> personList = new ArrayList<Person>(); 
     PersonList.add(new Person("1", "firstName1", "secondName1", "age1")); 
     PersonList.add(new Person("2", "firstName2", "secondName2", "age2")); 
     PersonList.add(new Person("3", "firstName3", "secondName3", "age3")); 
     PersonList.add(new Person("4", "firstName4", "secondName4", "age4")); 
     PersonList.add(new Person("5", "firstName5", "secondName5", "age5")); 

     this.pl.setPersonList(PersonList); 
    } 


    public PersonList getPersons() { 
     return this.pl; 
    } 


    public Person getPerson(final String id) { 
     return this.pl.getPerson(id); 
    } 


    public List<Person> updatePerson(final String id, final Person Person) { 
     return this.pl.updatePerson(id, Person); 
    } 

} 

public class PersonList { 

    private List<Person> personList = new ArrayList<Person>(); 


    public List<Person> getPersonList() { 
     return this.personList; 
    } 


    public void setPersonList(List<Person> PersonList) { 
     this.personList = personList; 
    } 


    public int getPersonIndex(Person person) { 
     int index = -1; 

     for (int i = 0; i < this.PersonList.size(); i++) { 
     if (this.personList.get(i).getId().equals(person.getId())) 
      index = i; 
     } 

     return index; 
    } 


    public Person getPerson(String id) 
    { 
     Person person = new Person(); 
     for (Person p : this.personList) { 
     if (p.getId().equals(id)) 
      return p; 
     } 
     return person; 
    } 


    public List<Person> updatePerson(String id, Person p) { 
     final Person person = this.getPerson(id); 
     final int personIndex = this.getPersonIndex(person); 

     person.setFirstName(p.getFirstName()); 
     person.setSecondName(p.getSecondName()); 
     person.setAge(p.getAge()); 

     this.personList.set(personIndex, person); 

     return this.personList; 
    } 

} 

而且在服務的請求:

@PUT 
    @Consumes(MediaType.APPLICATION_JSON) 
    @Path("/update/{id}") 
    public Response updatePerson(@PathParam("id") String id, Person p) { 
     Person p = this.provider.getPerson(id); 
     if (p.getd() != null) { 
     Person person = new Person(id, p.getFirstName(), f.getSecondName(), p.getAge()); 
     return Response.ok(this.provider.updatePerson(id, person), MediaType.APPLICATION_JSON).build(); 
     } 

     return Response.ok("Person doesn't exist", MediaType.APPLICATION_JSON).build(); 
    } 

我的問題是,當我做了更新,它返回與新值更新的列表中,但是當我再次爲列表getAll回到舊列表。

任何想法,我做錯了?

回答

1

我已經重寫了您的代碼以刪除PersonList類,我認爲這是不必要的,因爲大部分功能都由Map s覆蓋。在這種情況下,我使用LinkedHashMap,它具有可預測的順序。所有的

public class Provider { 

    private final Map<String, Person> mapOfPersons; 

    public Provider() { 
     this.mapOfPersons = new LinkedHashMap<String, Person>(); 

     for (int id = 1; id <= 5; id++) { 
      final String idString = Integer.toString(id); 
      mapOfPersons.put(idString, new Person(idString, "firstName" + id, "secondName" + id, "age" + id)); 
     } 
    } 

    public Map<String, Person> getMapOfPersons() { 
     return mapOfPersons; 
    } 

    // Since we're operating directly on the person that is in the map you don't need to upate the map. 
    public Map<String, Person> updatePerson(String id, Person p) { 
     final Person person = this.mapOfPersons.get(id); 

     person.setFirstName(p.getFirstName()); 
     person.setSecondName(p.getSecondName()); 
     person.setAge(p.getAge()); 

     return this.mapOfPersons; 
    } 

    // Probably you'll never use this. 
    public int getPersonIndex(Person person) { 
     return new ArrayList<String>(mapOfPersons.keySet()).indexOf(person.getId()); 
    } 
} 
1

首先,你應該檢查你的代碼,以簡化並消除某些虛假的問題,如:

  • 實例化未使用的變量(personListProvider構造函數)。
  • 找不到標識符時返回虛擬對象(getPerson)。
  • 對於corse,你最好把自己不需要的抽象保存爲PersonList,其中Map就足夠了(正如@Craig敏感地建議的那樣)。

但我認爲,根本原因是這種方法:

public void setPersonList(List<Person> PersonList) { 
     this.personList = personList; 
    } 

注意,它什麼都不做。

好的做法,以避免這種失誤是採取命名的護理:

  • 類名稱應以大寫字母(PersonList)開始進行命名。
  • 變量名稱應該以小寫字母開頭(personList)。