Skip to content

Latest commit

 

History

59 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REST Framework Comparison

If you can freely choose a language and framework when implementing a REST server application that deals with JSON body, what would you choose? This is a collection of working codes in various languages with frameworks tested on MacOS and Linux (Ubuntu).

They all work stand-alone. Integration with other frameworks is not necessary.

Note that these are just codes. Depending on the language, Java in particular, you may need a lot more than the codes, like project and build files. Other languages usually require installation of additional packages like the framework to be used.

So which one do you like?

Testing

The test script posts a JSON text to the REST server like this.

#!/bin/sh

PORT=18080

curl -X POST -H "Content-Type: application/json" -d '{"name":"Taro"}' http://localhost:${PORT}/hello

Then the server parses the JSON body and returns a greeting in plain text.

Hello Taro

Codes

It is quite amazing that the amount of codes as little as these, and without huge configuration files, can run fully functioning web servers serving dynamic web contents. My first exposure to such a system was when I used Apache httpd-1.3.x with Perl CGI in Y2K. It was much messier back then. :)

Here we go.

Framework: spark-11

package Spark11Sample;

import static spark.Spark.*;
import org.json.JSONObject;

public class App {
    public static void main(String[] args) {
        port(18080);
        post("/hello", (req, res) -> {
            JSONObject body = new JSONObject(req.body());
            return "Hello " + body.getString("name");
        });
    }
}

Framework: express

const express = require('express')
const bodyParser = require('body-parser');
const app = express()
const port = 18080

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/hello', (req, res) => res.send('Hello ' + req.body.name))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Framework: Ktor

import io.ktor.server.netty.*
import io.ktor.server.routing.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.request.*
import io.ktor.server.engine.*

import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.plugins.contentnegotiation.*

import kotlinx.serialization.Serializable

@Serializable
class Greeting {
    val name: String = ""
}

fun main(args: Array<String>) {
    embeddedServer(Netty, 18080) {
        install(ContentNegotiation) {
            json()
        }

        routing {
            post("/hello") {
                val greeting = call.receive<Greeting>()
                call.respondText("Hello, ${greeting.name}")
            }
        }
    }.start(wait = true)
}

Framework: Mojolicious

use Mojolicious::Lite;

post '/hello' => sub {
    my $c = shift;
    my $hash = $c->req->json;
    $c->render(text => 'Hello ' . $hash->{name}, format=>'txt');
};

my $port   = 18080;
my $daemon = Mojo::Server::Daemon->new(
  app    => app,
  listen => ["http://*:$port"]
);
$daemon->run;

Framework: Flask

from flask import Flask, request
app = Flask(__name__)

@app.route('/hello', methods=['POST'])
def hello():
    data = request.get_json()
    return "Hello " + data['name']

app.run(host='0.0.0.0', port=18080, debug=True)

Framework: Sinatra

require 'sinatra'

set :port, 18080

post '/hello' do
  request.body.rewind
  data = JSON.parse request.body.read
  "Hello #{data['name']}!"
end

More Frameworks

Restlet

This is a Java REST framework by Talend. While it requires a little more codes than Spark even for a small example like this, an external configuration file is not necessary. It has a build-in web server, so integration with another framework is not required.

Spring Boot

I could stay away from Spring Framework ever since I first saw application codes on top of it. However, Spring Boot seemed easier, so why not. It can automatically add Tomcat into your app. No need to set up a separate servlet container. Annotations make codes simple and clean. Worth a try.

Drogon

Drogon is a C++ framework that can process REST. It generates template codes for you, from where you can start implementing your app logic.

About

Comparing REST frameworks in various languages on MacOS and Linux

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages