0

I wrote a REST web service which is returning JSON as below

[{"id":0,"name":"Vishal","age":"23","dob":"21/1/1992","phone":"9966558","sslc":"90","hsc":"90","college":"90"}, 
{"id":0,"name":"Karthik","age":"27","dob":"14/8/1988","phone":"995674","sslc":"99","hsc":"100","college":"100"},
{"id":0,"name":"Jeeva","age":"29","dob":"10/1/1987","phone":"77422","sslc":"99","hsc":"99","college":"100"},
{"id":0,"name":"Arya","age":"26","dob":"10/1/1989","phone":"55668","sslc":"100","hsc":"99","college":"99"}]

But I want the output with the "student" appended as below.

{"student":[{"id":0,"name":"Vishal","age":"23","dob":"21/1/1992","phone":"9966558","sslc":"90","hsc":"90","college":"90"}, 
{"id":0,"name":"Karthik","age":"27","dob":"14/8/1988","phone":"995674","sslc":"99","hsc":"100","college":"100"},
{"id":0,"name":"Jeeva","age":"29","dob":"10/1/1987","phone":"77422","sslc":"99","hsc":"99","college":"100"},
{"id":0,"name":"Arya","age":"26","dob":"10/1/1989","phone":"55668","sslc":"100","hsc":"99","college":"99"}]}

how can I achieve this output?

Below is the Product Class

@XmlRootElement(name="student")
public class Student implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

public Student() {
    super();
}

public Student(int id, String name, String age, String dob, String phone,
        String sslc, String hsc, String college) {
    super();
    this.id = id;
    this.name = name;
    this.age = age;
    this.dob = dob;
    this.phone = phone;
    this.sslc = sslc;
    this.hsc = hsc;
    this.college = college;
}


private int id;
private String name;
private String age;
private String dob;
private String phone;

private String sslc;
private String hsc;
private String college;

@XmlElement
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

@XmlElement
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

@XmlElement
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}

@XmlElement
public String getDob() {
    return dob;
}
public void setDob(String dob) {
    this.dob = dob;
}

@XmlElement
public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}

@XmlElement
public String getSslc() {
    return sslc;
}
public void setSslc(String sslc) {
    this.sslc = sslc;
}

@XmlElement
public String getHsc() {
    return hsc;
}
public void setHsc(String hsc) {
    this.hsc = hsc;
}

@XmlElement
public String getCollege() {
    return college;
}
public void setCollege(String college) {
    this.college = college;
}
@Override
public String toString() {
    return "Student [id=" + id + ", name=" + name + ", age=" + age
            + ", dob=" + dob + ", phone=" + phone + ", sslc=" + sslc
            + ", hsc=" + hsc + ", college=" + college + "]";
}
}

Below is the service class.

@GET
@Path("/student.srv")
@Produces("application/json")
public Response getStudentJson(){
    DAOLayer daoLayer=new DAOLayer();
    List<Student> studentsList=null;
    try {
        studentsList=daoLayer.getStudents();

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return Response.ok(studentsList).build();
}

Kindly help me to achieve the above mentioned output. Thanks in Advance.

Kaushi
  • 198
  • 3
  • 8
  • 20

1 Answers1

4

To get the desired output, you will have to create one single root object containing a List<Student> student and return it:

Root.java

@XmlRootElement(name="root")
public class Root implements Serializable {

    @XmlList
    private List<Student> student = new ArrayList<Student>();

    // getter and setter
}

Service.java

@GET
@Path("/student.srv")
@Produces("application/json")
public Response getStudentJson(){
    DAOLayer daoLayer=new DAOLayer();
    List<Student> studentsList=null;
    try {
        studentsList=daoLayer.getStudents();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    Root root = new Root();
    root.setStudent(studentsList),

    return Response.ok(root).build();
}
Smutje
  • 17,733
  • 4
  • 24
  • 41