When the create-app command completes, Grails will create a mongodb-example directory with an application configured to create a REST application by default (using the rest-api profile) and configured to use the mongodb feature.

Once you have the application create you need to startup MongoDB. Typically this is done via the MONGODB_HOME/bin/mongod executable:

3 Writing the Application

Now you are ready to start writing the application.

3.1 Configure the Application

The first step after starting the MongoDB server is to ensure the application is correctly configured. By default the configuration for the MongoDB client can be found in the grails-app/conf/application.yml file:

3.3 Apply Domain Constraints

If you wish to define validation constraints
on properties defined in a GORM domain class, you can do so using the constraints property:

grails-app/domain/demo/Product.groovy

static constraints = {
name blank: false
price range: 0.0..1000.00
}

The above example applies two constraints:

The name property is constrained so that it cannot be a blank string.

The price property is constrained so that it must be greater than 0 and less than a thousand using the range constraint.

To verify these constraints work to our expectation you can write a test. If you used the create-domain-class a test
called src/test/groovy/demo/ProductSpec.groovy was generated for you already. Otherwise simply creating an appropriately
named test in src/test/groovy using your IDE or text editor will suffice.

Tests in Grails are written with Spock Framework,
tests including having spaces in the test name.

To write unit tests with MongoDB and Spock, you can simply extend from grails.test.mongodb.MongoSpec.

The RestfulController super class will implement all the necessary operations to perform the common REST verbs such as GET, POST, PUT and DELETE. If is you wish to override or forbid certain verbs you can override the equivalent method from the super (for example the delete method for DELETE) to return an alternative or forbidden response.

3.5 Map the Controller to a URI

By default the controller will be exposed under the /product URI. This is due to the default grails-app/conf/demo/UrlMappings.groovy class:

The test performs a search by executing the search method, passing appropriate arguments and verifying the JSON written to the response.
The tests asserts the value of the json property of the response. The resulting JSON rendered by Grails looks like:

[{"id":1,"name":"Apple","price":2.0}]

Let’s take a look at how we can customize this JSON.

3.8 Customizing the JSON Output

Grails uses JSON Views to represent render JSON responses. The idea being to continue the philosophy of separating the controller logic from the view logic.

The default view rendered if no specific view is found is grails-app/views/object/_object.gson:

grails-app/views/object/_object.gson

importgroovy.transform.*@FieldObject object
json g.render(object)

The default _object.gson view simply uses the g.render(..) method to automatically product a JSON representation of the object.

If you want to alter the output of the JSON the way that is done in Grails is by creating a view. You can generate a starting point with the generate-views command of the CLI: