bittersour bittersour - 2 months ago 11
reST (reStructuredText) Question

Parsing JSON to REST api

I parsed the below json string to REST api. When I did a printline, only the first get printed out. How can I make it to the expected result?

json input

{
"age": "10",
"name": "Daniel"
},
{
"age": "12",
"name": "Fedrik"
}


Student.java

package pagecode.module.jaxrs;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Student
{
private String name;
private int age;

public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}


TestingResource.java

package pagecode.module.jaxrs;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

@Path("/testing")
public class TestingResource {
@Path("json")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void consumeJSONRequest(Student s)
{
System.out.println("**Received JSON request**");
System.out.println(" Name : "+s.getName());
System.out.println(" Age : "+s.getAge());
}
}


Current Printline

**Received JSON request**
Name : Daniel
Age : 10


Expected Printline

**Received JSON request**
Name : Daniel
Age : 10
Name : Fedrik
Age : 12

Answer

You need to use List<Student> as a parameter. Then you can only get those data.