You can define your service method as follows, using Person POJO:
@GET
@Produces("application/json")
@Path("/list")
public String getList(){
List<Person> persons = new ArrayList<>();
persons.add(new Person("1", "2"));
persons.add(new Person("3", "4"));
persons.add(new Person("5", "6"));
// takes advantage to toString() implementation to format as [a, b, c]
return persons.toString();
}
The POJO class:
@XmlRootElement
public class Person {
@XmlElement(name="fn")
String fn;
@XmlElement(name="ln")
String ln;
public Person(){
}
public Person(String fn, String ln) {
this.fn = fn;
this.ln = ln;
}
@Override
public String toString(){
try {
// takes advantage of toString() implementation to format {"a":"b"}
return new JSONObject().put("fn", fn).put("ln", ln).toString();
} catch (JSONException e) {
return null;
}
}
}
The results will look like:
[{"fn":"1","ln":"2"}, {"fn":"3","ln":"4"}, {"fn":"5","ln":"6"}]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…