Search code examples
javajavascriptrestauto-generateboilerplate

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?


Solution

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