N
N
nadom2016-04-11 22:01:45
Java
nadom, 2016-04-11 22:01:45

Getting JSON array in RestEasy server?

A small tutorial Rest web service.
I use RestEasy + glassfish.
GET methods to get one record (works) and all records (does not work).
What did he do wrong?

Here is the working method:

@GET
  @Path("/person/{id}")
    @Produces("application/json")
  public Person getPerson(@PathParam("id") int id) {
    System.out.println("get person by id");
    MySQLConnection myConnection
      = new MySQLConnection("jdbc:mysql://localhost/pisl", "root", "root");
    Person response = myConnection.selectPerson(id);
    
    myConnection.close();
        return response;	
  }


Here is the non-working method:
@GET
  @Path("/person")
    @Produces("application/json")
  public List<Person> getPersons() {
    System.out.println("get all persons");
    MySQLConnection myConnection
      = new MySQLConnection("jdbc:mysql://localhost/pisl", "root", "root");
    List<Person> response = myConnection.selectPersons();
    
    myConnection.close();
        return response;	
  }


Here is the class itself:
public class Person {
  public Person() {};
  
  @Override
  public String toString() {
    String json = new StringBuffer("idperson: ").append(idperson)
        .append("firstname: ").append(firstName)
        .append("secondname: ").append(secondName)
        .append("gender: ").append(gender)
        .append("dateofbirth: ").append(dateOfBirth.toString())
        .append("phone: ").append(phone)
        .append("city: ").append(city)
        .append("faculty: ").append(faculty).toString();
    
    return json;
  }
  
  public int getIdperson() {
    return idperson;
  }
  public void setIdperson(int idperson) {
    this.idperson = idperson;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getSecondName() {
    return secondName;
  }
  public void setSecondName(String secondName) {
    this.secondName = secondName;
  }
  public boolean isGender() {
    return gender;
  }
  public void setGender(boolean gender) {
    this.gender = gender;
  }
  public java.sql.Date getDateOfBirth() {
    return dateOfBirth;
  }
  public void setDateOfBirth(java.sql.Date dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
  }
  public String getPhone() {
    return phone;
  }
  public void setPhone(String phone) {
    this.phone = phone;
  }
  public String getCity() {
    return city;
  }
  public void setCity(String city) {
    this.city = city;
  }
  public String getFaculty() {
    return faculty;
  }
  public void setFaculty(String faculty) {
    this.faculty = faculty;
  }
  private int idperson;
  private String firstName;
  private String secondName;
  private boolean gender;
  private java.sql.Date dateOfBirth;
  private String phone;
  private String city;
  private String faculty;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question