RESTful 畅玩!框架框架、RESTful

2023-09-06 10:46:16 作者:这世界唯一旳你-

我们正在计划一个主要为移动应用提供内容的项目,但需要有一个网站.

We are planning a project primarily serving content to mobile apps, but need to have a website.

我的问题是使用 Jersey 或 Restlet 为我们的移动应用程序开发 REST API,然后使用 Play!为网站服务.

My question is whether is makes sense to use Jersey or Restlet to develop REST APIs for our mobile apps, and then use Play! to serve the website.

还是只使用 Play 更有意义!做这一切?如果是这样,如何使用 Play 进行 REST!框架?

Or does it make more sense to just use Play! to do it all? If so, how to do REST with Play! framework?

推荐答案

根据要求,一个简单的类似 REST 的方法.它的工作方式与 Codemwncis 的解决方案几乎相同,但使用 Accept 标头进行内容协商.首先是路由文件:

As per request, a simple REST-like approach. It works almost the same way Codemwncis' solution works but uses the Accept header for content negotiation. First the routes file:

GET     /user/{id}            Application.user
POST    /user/                Application.createUser
PUT     /user/{id}            Application.updateUser
DELETE  /user/{id}            Application.deleteUser

您没有在此处指定任何内容类型.恕我直言,只有当您想要为某些资源拥有特殊"URI 时才需要这样做.就像声明到 /users/feed/ 的路由总是在 Atom/RSS 中返回一样.

You don't specify any content type here. Doing so is IMHO only necessary when you want to have "special" URIs for certain resources. Like declaring a route to /users/feed/ to always return in Atom/RSS.

应用程序控制器如下所示:

The Application controller looks like this:

public static void createUser(User newUser) {
    newUser.save();
    user(newUser.id);
}

public static void updateUser(Long id, User user) {
    User dbUser = User.findById(id);
    dbUser.updateDetails(user); // some model logic you would write to do a safe merge
    dbUser.save();
    user(id);
}

public static void deleteUser(Long id) {
    User.findById(id).delete();
    renderText("success");
}

public static void user(Long id)  {
    User user = User.findById(id)
    render(user);
}

如您所见,我只删除了 getUserJSON 方法并重命名了 getUser 方法.要使不同的内容类型起作用,您现在必须创建多个模板.每个所需的内容类型一个.例如:

As you can see I only removed the getUserJSON method and renamed the getUser method. For different content types to work you now have to create several templates. One for each desired content type. For example:

user.xml:

<users>
  <user>
    <name>${user.name}</name>
    . . .
  </user>
</users>

user.json:

{
  "name": "${user.name}",
  "id": "${user.id}",
  . . . 
}

user.html:

<html>...</html>

这种方法始终为浏览器提供 HTML 视图,因为所有浏览器都在其 Accept 标头中发送 text/html 内容类型.所有其他客户端(可能是一些基于 JavaScript 的 AJAX 请求)可以定义自己想要的内容类型.使用 jQuerys ajax() 方法,您可以执行以下操作:

This approach gives browsers always the HTML view, since all browsers send a text/html content type in their Accept header. All other clients (possibly some JavaScript-based AJAX requests) can define their own desired content type. Using jQuerys ajax() method you could do the following:

$.ajax({
  url: @{Application.user(1)},
  dataType: json,
  success: function(data) {
    . . . 
  }
});

这应该可以让您以 JSON 格式获取 ID 为 1 的用户的详细信息.Play 目前原生支持 HTML、JSON 和 XML,但您可以按照 官方文档 或使用内容协商模块.

Which should get you the details about User with the ID 1 in JSON format. Play currently supports HTML, JSON and XML natively but you can easily use a different type by either following the official documentation or use the content negotiation module.

如果您使用 Eclipse 进行开发,我建议您使用 REST 客户端插件,它可以让您测试您的路线和它们对应的内容类型.

If you are using Eclipse for development I suggest use the REST client plugin which lets you test your routes and their corresponding content type.