我嘗試用AppEngine上Restlet框架和我嘗試檢索對象的列表(ArrayList的爲例)與JAX-RS JSON表示資源需要的Restlet + JAX-RS + JSON很好的例子
對於實例的樣本用戶等級:
public class User {
private String lastname;
private String firstname;
public User(){};
public User(String lname,String fname){
this.firstname = fname;
this.lastname = lname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
我要找回這樣
@GET @Path("users")
@Produces(MediaType.APPLICATION_JSON)
public ArrayList<User> getUsers() {
ArrayList<User> users = new ArrayList<User>();
users.add(new User("userlastname1", "userfirstname1"));
users.add(new User("userlastname3", "userfirstname2"));
return users;
}
用戶的名單,我希望得到這個JSON表示
[
{
lastname: "userlastname1",
firstname: "userfirstname1"
},
{
lastname: "userlastname2",
firstname: "userfirstname2"
}
]
實現此目的的最佳方法是什麼?
的的Restlet文檔中提到的系列化proccess是自動的,是的,它只有一個類似「用戶」對象的情況下
new User("userlastname1", "userfirstname1")
還給我好JSON表示
{
lastname: "userlastname1",
firstname: "userfirstname1"
}
如果有人能給我一些解釋/示例代碼/想法。非常感謝
問候