RESTful on Play!骨架 [英] RESTful on Play! framework

查看:89
本文介绍了RESTful on Play!骨架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

user.json:

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

user.html:

user.html:

<html>...</html>

此方法为浏览器提供HTML视图,因为所有浏览器都会在其中发送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.

这篇关于RESTful on Play!骨架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆