Search code examples
grailsjpajax-rsodata

How do you expose a Grails domain model using OData?


Ideally there would be a plugin that automatically exposed the Grails domain model as OData but I can't see one.

There is OData4j which will let you expose POJOs or JPA as OData however it uses JAX-RS and Jersey under the covers and I'm not sure how to use that inside a Grails application.


Solution

  • I would use Apache Olingo. Follow their Java example and modify for Groovy/Grails like such:

    class DataController {
        def action() {
            // create odata handler and configure it with DemoEdmProvider and Processor
            def odata = OData.newInstance()
            def edm = odata.createServiceMetadata(new DemoEdmProvider(), [])
            def handler = odata.createHandler(edm)
            handler.register(new DemoEntityCollectionProcessor())
    
            // let the handler do the work
            handler.process(request, response)
            return false
        }
    }