需要RESTLET + JAX-RS + JSON 很好的例子

发布于 2025-01-06 10:12:53 字数 1552 浏览 3 评论 0原文

我正在使用 AppEngine 试验 Restlet 框架,并尝试使用 JAX-rs 资源将对象列表(例如 ArrayList)检索为 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 文档提到序列化过程是自动化的,是的,只有一个像“User”这样的对象

new User("userlastname1", "userfirstname1")

会返回给我良好的 JSON 表示,

{
    lastname: "userlastname1",
    firstname: "userfirstname1" 
}

如果有人能给我一些解释/示例代码/想法。非常感谢

最好的问候

I'm experimenting the Restlet framework with AppEngine and I try to retrieve a list of Object (ArrayList for instance) to a JSON representation with JAX-rs Resource

For instance a sample User class:

 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;
    }
 }

I want to retrieve a list of User like this

@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;
}

and I want to get this JSON representation

[
    {
        lastname: "userlastname1",
        firstname: "userfirstname1"
    },
    {
        lastname: "userlastname2",
        firstname: "userfirstname2"
    }
]

What is the best way to achieve this?

The Restlet documentation mentioned the serialization proccess is automated, yes it's the case for only one Object like "User"

new User("userlastname1", "userfirstname1")

return me the good JSON representation

{
    lastname: "userlastname1",
    firstname: "userfirstname1" 
}

If some one can give me some explanation/sample code/idea about that. much appreciated

Best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

公布 2025-01-13 10:12:54

实际上,我只是使用 Restlet 的 Java EE 版本自己解决了这个问题。不确定这个模块是否可用于 GAE 沙箱,但我刚刚添加了 Jackson 的 Restlet 扩展作为依赖项,它的工作就像一个魅力。

如果您使用 Maven:

<dependency>  
    <groupId>org.restlet.jee</groupId>  
    <artifactId>org.restlet.ext.jackson</artifactId>  
    <version>2.0.10</version>
</dependency>

I actually just figured this out for myself using the Java EE edition of Restlet. Not sure if this module is available for the GAE sandbox, but I just added the Restlet extension for Jackson as a dependency and it worked like a charm.

If you are using Maven:

<dependency>  
    <groupId>org.restlet.jee</groupId>  
    <artifactId>org.restlet.ext.jackson</artifactId>  
    <version>2.0.10</version>
</dependency>
给我一枪 2025-01-13 10:12:54
@Path("/users")
public interface IUserRoutes
{
    @GET
    Response getUsers();
}


@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class UserRoutesImpl implements IUserRoutes
{
    public Response getUsers()
    {
        return new UserResponse().getUsers();
    }
}


public class UserResponse
{
    public Response getUser()
    {
        List<Users> users = new UserModel().getUsers();
        return Response.ok(users).build();
    }
}

您的 User 对象应该是 POJO。

public class User
{
    private String firstName;
    private String lastName;

    public String getFirstname()
    {
        return firstName;
    }
    public String getLastname()
    {
        return lastName;
    }
    //etc 
}
@Path("/users")
public interface IUserRoutes
{
    @GET
    Response getUsers();
}


@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class UserRoutesImpl implements IUserRoutes
{
    public Response getUsers()
    {
        return new UserResponse().getUsers();
    }
}


public class UserResponse
{
    public Response getUser()
    {
        List<Users> users = new UserModel().getUsers();
        return Response.ok(users).build();
    }
}

Your User object should be a POJO.

public class User
{
    private String firstName;
    private String lastName;

    public String getFirstname()
    {
        return firstName;
    }
    public String getLastname()
    {
        return lastName;
    }
    //etc 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文