gpt4 book ai didi

java - 未使用 Java Web 服务更新的对象列表

转载 作者:行者123 更新时间:2023-12-02 04:24:11 24 4
gpt4 key购买 nike

我正在构建一个简单的网络应用程序,使用 java 中的 rest 服务。我想更新 Person 类型的对象列表。这是到目前为止我的代码:

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 时,我会返回旧列表。

知道我哪里做错了吗?

最佳答案

我稍微重写了您的代码以删除 PersonList 类,我认为这是不必要的,因为 Map 涵盖了大部分功能。在本例中,我使用的是具有可预测顺序的 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());
}
}

关于java - 未使用 Java Web 服务更新的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com