Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to generate boilerplate code for RESTful stacks?

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?

like image 549
Alex Feinman Avatar asked Jun 15 '12 15:06

Alex Feinman


3 Answers

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.

like image 104
Juan Enrique Muñoz Zolotoochin Avatar answered Nov 03 '22 19:11

Juan Enrique Muñoz Zolotoochin


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.

like image 1
Mike Avatar answered Nov 03 '22 21:11

Mike


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 ..
           }
        });
    }
}
like image 1
Matthew Ratzloff Avatar answered Nov 03 '22 20:11

Matthew Ratzloff