Scala 和 Play-Framework 公开 REST 服务。现在进行渲染(但需要使用 Scala 以外的其他东西)

发布于 2024-12-09 00:44:03 字数 406 浏览 0 评论 0原文

我有一项大学作业,有一个非常特殊的要求。 关键在于我们需要构建一个使用两种不同语言的 Web 应用程序。我知道奇怪的要求。

我立即想到也许可以让 Scala 和 Play Framework 以 JSON 格式提供数据,然后使用某种 Python 客户端,将 REST 服务呈现为 HTML。

问题是我对此很陌生。我以前从未做过 REST 的东西,甚至术语也令人畏惧。然而,我已经成功地使用 Play 启动并运行了几个模型,为 Json 提供服务。现在我需要渲染它。

您会建议什么来满足该要求?还有其他想法吗?理想情况下,我仍然想使用 Scala 和 Play,但除了这个限制之外,我不关心其他什么。

编辑:我知道这是一个奇怪的要求。为什么我不直接使用 Play 来渲染 HTML...?唉,我不能。

I have a university assignment with a very peculiar requirement. The crux of it is that we need to build a web application that utilizes 2 different languages. Weird requirement I know.

I immediately thought to perhaps have Scala and the Play Framework serving data in JSON, and then have some sort of Python client, render the REST services as HTML.

The issue is I am very new to this. I've never done REST stuff before, and even the terminology is daunting. I have however managed to get several models up and running with Play, serving the Json. Now I need to render it.

What would you recommend to satisfy that requirement? Any other ideas?. Ideally I would still like to use Scala and Play, but apart from that constraint I don't care what else.

Edit: I know it's a weird requirement. Why wouldn't I just use Play to render the HTML...? Alas I can't.

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

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

发布评论

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

评论(4

迷爱 2024-12-16 00:44:03

我创建了一个非常简单的项目来演示如何执行此操作:
https://github.com/jamesward/playscalapython

没有太多内容。这是游戏! / Scala 应用程序:

package controllers

import play._
import play.mvc._

object Application extends Controller {

    def index = {
        val widget1: Widget = Widget(1, "The first Widget")
        val widget2: Widget = Widget(2, "A really special Widget")
        val widget3: Widget = Widget(3, "Just another Widget")
        val widgets: Vector[Widget] = Vector(widget1, widget2, widget3)
        Json(widgets.toArray)
    }

}

case class Widget(val id: Int, val name: String)

这是使用来自 Play! 的 JSON 的 Python 应用程序。应用程序:

import os
import simplejson
import requests
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    jsonString = requests.get(os.environ.get("JSON_SERVICE_URL", "http://localhost:9000"))
    widgets = simplejson.loads(jsonString.content)
    htmlResponse = "<html><body>"
    for widget in widgets:
        htmlResponse += "Widget " + str(widget['id']) + " = " + widget['name'] + "</br>"
    htmlResponse += "</body></html>"
    return htmlResponse

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port)

I created a very simple project that shows how to do this:
https://github.com/jamesward/playscalapython

There isn't much to it. Here is the Play! / Scala app:

package controllers

import play._
import play.mvc._

object Application extends Controller {

    def index = {
        val widget1: Widget = Widget(1, "The first Widget")
        val widget2: Widget = Widget(2, "A really special Widget")
        val widget3: Widget = Widget(3, "Just another Widget")
        val widgets: Vector[Widget] = Vector(widget1, widget2, widget3)
        Json(widgets.toArray)
    }

}

case class Widget(val id: Int, val name: String)

Here is the Python app that consumes the JSON from the Play! app:

import os
import simplejson
import requests
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    jsonString = requests.get(os.environ.get("JSON_SERVICE_URL", "http://localhost:9000"))
    widgets = simplejson.loads(jsonString.content)
    htmlResponse = "<html><body>"
    for widget in widgets:
        htmlResponse += "Widget " + str(widget['id']) + " = " + widget['name'] + "</br>"
    htmlResponse += "</body></html>"
    return htmlResponse

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port)
自由如风 2024-12-16 00:44:03

客户端-服务器应用程序可以满足您的目的吗?如果一部分是 Java 代码而另一部分是简单的 C# GUI,那么通过 XML 进行通信没有什么区别吗?有许多不同的解决方案可用。

实际上,您甚至不需要复杂的 XML 解决方案:在您的目的中使用 Hessian 是小菜一碟。它是一个二进制 Web 服务,并且具有可用的实现,例如。对于 Java 和 C++。

Could a client-server application do for your purposes? Communicating through XML makes no difference if one part is a Java code and another an easy C# GUI? There are many different solutions available.

Actually, you don't even need a complicated XML solution: A piece of cake would be using of Hessian in your purposes. It is a binary Web Service and it has implementations available eg. for Java and C++.

高冷爸爸 2024-12-16 00:44:03

首先,简单介绍一下休息的背景。它实际上只是一种表示您的 url 标识您的资源的方式,并且您对这些 url 执行的 HTTP 操作指定相应的 CRUD 操作。

例如,您有一家书店。如果您想列出所有书籍,请访问 http://bookstore.com/books。如果您想查看单本书的详细信息,您可以在 http://bookstore.com 上执行 HTTP GET /books/BOOK_NAME

如果您想创建一本新书,您需要在 http://bookstore.com/books/ 上执行 HTTP POST NEW_BOOK_NAME 包含新图书数据。

同样,要更新一本书,您需要执行 HTTP PUT,要删除一本书,您需要执行 HTTP DELETE。请注意,并非所有浏览器都支持所有 http 操作,因此很多时候 Restful Web 应用程序用于服务或机器对机器通信。

您可以使用 play 来服务器 json 并使用 swing 来构建 GUI。如果你想使用 Scala,它有相当不错的摇摆支持。

从 Scala/java 中的 Restful Web 服务请求数据将非常简单。您可以使用内置的 java.net、第三方库(如 apache httpClient),也可以使用 Scala io.Source - Source.fromURL("http://server/resource")。

此外,Scala 内置了对 XML 的支持,如果数据是 XML,这将使使用数据变得容易。

还有另一种可能。你可以采取尖刻的方法。让框架提供 html + javascript。 Javascript 绝对是另一种编程语言。您可以选择 jquery、prototype 或 extjs 之类的东西来帮助构建您的前端。

我知道我有点胡言乱语,但我希望这会有所帮助。

First, a simple background on rest. It's really nothing but a way to say that your url identifies you resources and the HTTP actions you perform on those urls specify the corresponding CRUD operations.

For example, you have a bookstore. If you want to list all books, you'd visit http://bookstore.com/books. If you want to view the details for a single book, you'd perform an HTTP GET on http://bookstore.com/books/BOOK_NAME.

If you want to create a new book, you'd perform an HTTP POST on http://bookstore.com/books/NEW_BOOK_NAME with the new book data.

Similarly, to update a book, you'd perform an HTTP PUT and to delete a book you'd perform an HTTP DELETE. Note that not all browsers support all of the http actions, so many times restful web applications are for services or machine to machine communication.

You could use play to server the json and use swing to build a gui. If you want to use Scala, it has pretty decent swing support.

Requesting data from a restful web service in Scala/java would be pretty simple. You can use the built-in java.net, a 3rd party library like apache httpClient, or you could use Scala io.Source - Source.fromURL("http://server/resource").

Also, Scala has built-in support for XML, which would make consuming data easy if it were XML.

There is another possibility. You could take the snarky approach. Have the framework serve up html + javascript. Javascript is definitely another programming language. You could pick something like jquery, prototype, or extjs to help build your front-end.

I know I rambled a bit, but I hope this helps.

那伤。 2024-12-16 00:44:03

您有数据库后端并且使用 SQL 访问数据库吗?这可以被视为你的第二语言。

Do you have a database backend and are you using SQL to access the database? That could be considered your second language.

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