开发者

Scala and Play-Framework exposing REST services. Now to render (but need to use something other then Scala)

开发者 https://www.devze.com 2023-04-11 22:08 出处:网络
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 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 开发者_StackOverflowwould 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.


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)


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++.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号