Monday, February 9, 2015

Grails - RESTful web services and Boostrap

Just started looking at RESTful web services and I have learned a valuable lesson about Bootstrap.

As I have just started out with Grails I am still not that familiar with what Grails is doing in the background, no matter how obvious it might be. One such case is the initialization of the application context.  I recently created a (RESTful) controller that uses a service.  As the RESTful side of things is not currently secured, no context initialization (or service initialization) is performed.  I will be looking at securing things shortly so that may change the state of affairs but for the moment, I need to initialize my service in Bootstrap.groovy:

class BootStrap {
    // declare service if used outside the context of the running application
    def someService
    def init = { servletContext ->
        ...

Otherwise, I get something like:

Method on class [Person] was used outside of a Grails application. 
If running in the context of a test using the mocking API or bootstrap Grails correctly.

The reference to testing is a good hint - something that would normally be initialized by Grails has not yet been initialized!