10

As I get more into RESTful APIs, the (good) simplicity of the approach means that you generate a LOT of boilerplate code, and code that has to match in three or four different places, e.g. for a Jersey-based stack:

  1. HTML on the web page which provides controls (a button Create Foo)
  2. JS on the web page which formulates the JSON and the request to create a Foo
  3. A FooRest class method to handle the request and create a Foo
  4. A Foo class to instantiate, which will manipulate the data structure

Are there tools which provide a starting point for some or all of this code automatically, possibly starting from something straightforward like a JSON data structure? E.g., provide:

card: {
  methods: [GET],
}
handOfCards: {
  methods: [GET POST PUT DELETE],
}

and at the very least end up with Ajax requests, CardRest and HandOfCardsRest classes with the specified methods stubbed out, and Card and HandOfCards classes with properties or getters/setters?

Alex Feinman
  • 5,393
  • 1
  • 30
  • 48

5 Answers5

3

Have you tried Backbone.js? It is a JavaScript library that handles REST Ajax requests for you. It allows you to define your models to wrap the data and provides setters, getters, save and delete functions, etc.

It also allows you to bind the models to views which generate the UI HTML.

  • Hmm, interesting. It's definitely half-way there. That plus a smaller perl script might work. ;) – Alex Feinman Jun 20 '12 at 14:22
  • I hadn't really looked at backbone before, but it seems this would be an awesome frontend for a grails backend, which I mention in my answer below. – Mike Jun 22 '12 at 22:43
1

Your goal should probably not be code generation of boilerplate but actually writing less code.

Spark is a Java micro web framework based on Sinatra.

Here's some example code:

import static spark.Spark.*;
import spark.*;

public class HelloWorld {
    public static void main(String[] args) { 
        get(new Route("/") {
           @Override
           public Object handle(Request request, Response response) {
              // .. Show something ..
           }
        });

        post(new Route("/") {
           @Override
           public Object handle(Request request, Response response) {
              // .. Create something .. 
           }
        });

        put(new Route("/") {
           @Override
           public Object handle(Request request, Response response) {
              // .. Update something ..
           }
        });

        delete(new Route("/") {
           @Override
           public Object handle(Request request, Response response) {
              // .. annihilate something ..
           }
        });

        options(new Route("/") {
           @Override
           public Object handle(Request request, Response response) {
              // .. appease something ..
           }
        });
    }
}
Matthew Ratzloff
  • 4,518
  • 1
  • 31
  • 35
  • Can you help me understand how this helps build the front and back ends of a RESTful framework? I'm missing something... – Alex Feinman Jun 20 '12 at 19:20
  • It simplifies the back end quite a bit. Rails is a simple and direct solution for your immediate problem (e.g., `rails generate scaffold`), but you said you're using Jersey currently so I thought you might prefer a Java-based solution. Having used Jersey, I would wholeheartedly recommend something like Spark over it. – Matthew Ratzloff Jun 20 '12 at 19:38
1

An alternate (or addition) to Juan's answer, you might want to check out Knockback.js , which takes the best of Knockout and adds the best of Backbone.js . Backbone has support for RESTful API's via it's "sync" functions. Quoting their website:

The method signature of Backbone.sync is sync(method, model, [options])

method – the CRUD method ("create", "read", "update", or "delete")
model – the model to be saved (or collection to be read)
options – success and error callbacks, and all other jQuery request options
Community
  • 1
  • 1
blong
  • 2,815
  • 8
  • 44
  • 110
1

I think nearly any *rails application does all of this for you. Grails is my favorite right now, and once you get the initial setup done (a single command) you create domain classes with another command.

Once those are created, you can generate both views (html) and controllers for handling all of these actions with a single command, and the boiler plate is sufficient for a lot of initial sites. It will even create test cases for you, although you'll need to define what the actual tests do. You can program it by convention very easily, or create your own mappings from URLs -> controller actions. It has a ton of plugin support and easily handles remote submission of forms (via javascript) built in.

It doesn't take a json data structures for creation, but the domains are very easily created (using groovy) and it autowires getter/setters, service injections, etc as it is based on the Spring Framework.

Mike
  • 611
  • 4
  • 12
  • I wanted slightly more control than *rails had given me, but I guess this is still closest to the right answer. :-/ – Alex Feinman Jun 24 '12 at 19:36
0

You might want to try a different approach altogether and try somethink like project lombok. It will at least let you nix all your getters and setters.

Nathan Feger
  • 19,122
  • 11
  • 62
  • 71
  • That's a start, though actually my IDE automagically inserts the getters/setters. But it's all the annotations and methods that's galling me. Maybe I'll just write a perl script. ;) – Alex Feinman Jun 15 '12 at 18:41
  • This does not really answer the question. This has absolutely nothing to do with REST, whatsoever. – W. Goeman Jun 16 '12 at 10:03