Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

RESTful GORM acces

padcom edited this page Jan 9, 2012 · 1 revision

This document is supposed to be a draft for the development of a feature of Grails that'll allow accessing GORM-managed entities via a RESTful interface.

  1. General idea

  2. Possible approaches

  3. Examples

  4. General idea

The general idea is to have a selected set of domain classes accessible over HTTP using default HTTP methods like GET, PUT, POST and DELETE.

  1. Possible approaches

Accessing domain classes might be done in different ways. For example, to make it universal, one might use "where queries" to fetch the data. It'll work as long as the path it up to the first level property of a domain class exposed via REST. For a deeper-level penetration a different approach is needed. In the case of a universal access the elements of a URI might be parsed and retrieved level-by-level. That forces to create more queries than one to retrieve the resource from a datastore.

In the case of a NoSQL datastore like MongoDB it might be the only solution anyways. They don't allow directly for querying relations.

In the case of a relational datastore it'd be possible to use inner joins to retrieve the exact amount of data directly.

  1. Examples

Assuming the following domain classes

class Person {
    Long id
    String firstName
    String lastName

    static hasMany = [ address: Address ]
}

class Address {
    Long id
    String city
    String street
}

the following operations should be possible:

Retrieve all person instances
    GET /person
Retrieve person instances with ID 1
    GET /person/1
Retrieve person with ID 1 firstName field 
    GET /person/1/firstName
Retrieve addresses of person with ID 1 
    GET /person/1/address
Retrieve address with ID 1 of person with ID 1 
    GET /person/1/address/1
Retrieve city property address with ID 1 of person with ID 1 
    GET /person/1/address/1/city
Create a new person
    POST /person
Replace or create the person instance with ID 1
    PUT /person/1
Change the firstName property of person instance with ID 1 (fail if instance does not exist)
    PUT /person/1/firstName
Replace all addresses of person instance with ID 1 (fail if instance does not exist)
    PUT /person/1/address
Delete a person with ID 1
    DELETE /person/1
Delete addresses of a person with ID 1
    DELETE /person/1/address
Replace all person instances
    PUT /person
Delete all person instances
    DELETE /person

Obviously this list is not complete and a lot of other combination is possible. It does however underline the way the interface should work.

Generally the idea is to utilize the domain model to expose a full RESTful API for the application. It is OK to have it done for some domain classes, not for all therefore this option needs to be configurable.

Clone this wiki locally