Scala and Play-Framework providing REST services. Now do it (but you need to use something else and then Scala)

I have a university assignment with a very peculiar requirement. The bottom line is that we need to create a web application that uses 2 different languages. A strange claim that I know.

I immediately thought that there might be Scala, and the Play Framework serves the data in JSON, and then it has a kind of Python client rendering the REST services as HTML.

The problem is that I am very new to this. I had never done REST before, and even the terminology is complicated. However, I managed to run several models from Play, serving Json. Now I need to do this.

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

Edit: I know this is a strange requirement. Why don't I just use Play to render HTML ...? Alas, I can’t.

+5
source share
4 answers

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

It's not that much. Here is the game! / Scala application:

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 a Python application that consumes JSON from Play! Application:

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)
+7
source

- ? XML , Java, - # GUI? .

XML-: Hessian . -, , . Java ++.

+1

-, . , , url HTTP, URL, CRUD.

, . , http://bookstore.com/books. , HTTP GET http://bookstore.com/books/BOOK_NAME.

, HTTP POST http://bookstore.com/books/NEW_BOOK_NAME .

, , HTTP PUT , HTTP DELETE. , , http, , - .

play json swing gui. Scala, .

- Scala/java . java.net, , ​​ apache httpClient, Scala io.Source - Source.fromURL( "http://server/resource" ).

, Scala XML, , XML.

. . html + javascript. Javascript . - jquery, prototype extjs, .

, , , .

+1

Do you have a database and use SQL to access the database? This can be considered your second language.

0
source

All Articles