Display specific element from list via REST



I have some problems with displaying specific element from ArrayList vai REST. When I display all elements from list is ok, but when I try to display specific element (e.g. first from the list), I have error:



org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo SEVERE: MessageBodyWriter not found for media type=text/xml, type=class .......Model, genericType=class .....Model.



I tried to set @XmlRootElement in model, but than I can not display anything. How can I change my code to fix this problem?



Model:



public class Model {
private String nin;
private String surname;
private String name;

public Model(String nin, String surname, String name) {
super();
this.nin = nin;
this.surname = surname;
this.name = name;
}
//getters & setters
}


List of persons:



@XmlRootElement(name = "persons")
public class ListOfPersons {

List<Model> personList = new ArrayList<Model>();

ListOfPersons(){
personList.add(new Model("92", "K", "S"));
personList.add(new Model("94", "C", "O"));
}

public List<Model> getList(){
return personList;
}

public void setList(List<Model> list){
this.personList=list;
}
}


Rescource:



@Path("v1")
public class Resource {

public Resource() {}

@Path("/get")
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public ListOfPersons getListOfPersons() {
ListOfPersons list = new ListOfPersons();
System.out.print(list);
return list;
}

@Path("/gets/{id}")
@GET
@Produces("text/xml")
public Model showPerson(@PathParam("id") int id) {
ListOfPersons list = new ListOfPersons();
Model person = list.getList().get(id);

System.out.println(list.getList().get(id));
return person;
}
}

No comments:

Post a Comment