We are using Grammarly to help us writing this article

Grails 3, MongoDB and REST API Profile CRUD Application

by Didin J. on Jan 28, 2018 Grails 3, MongoDB and REST API Profile CRUD Application

Step by step tutorial of building CRUD REST API using Grails 3, MongoDB and REST-API profile in easy way.

Step by step tutorial of building CRUD REST API using Grails 3, MongoDB and REST-API profile in an easy way. This tutorial just another example of Grails 3 REST-API profile with MongoDB Feature, actually there are same guides from Grails Guides. Previously, we have shown you how to create Grails 3 REST API using regular Grails 3 application and Grails 3 Angular 5 profile. Now, we will build a pure REST API CRUD application using Grails 3, MongoDB and REST-API profile.

The following tools, frameworks, and library are required for this tutorial:

- Grails 3 (We are using the latest Grails 3.3.2)
- MongoDB
- Terminal or Command Line
- IDE or Text Editor

Please make sure that you already download and install Grails 3.3.2 and MongoDB before continue to the next steps.


1. Create a new Grails 3 Application with REST-API Profile

As usual, we always start the steps of the tutorial from zero. Open the terminal or command line then go to your Grails project folder. Type this command to create a new Grails 3 application.

grails create-app grails-rest --profile=rest-api --features=mongodb

That command will create a Grails 3.3.2 application with REST-API profile and additional features MongoDB. Next, go to the newly created application folder.

cd ./grails-rest

Next, enter Grails interactive console by type this command.

grails

For sanitation, run the application for the first time by type this command in the Grails interactive console.

run-app

And you will see JSON response when you open the `localhost:8080` from the browser.

Grails 3, MongoDB and REST API Profile CRUD Application - Grails in the browser


2. Configure MongoDB plugin

Because MongoDB already added to the project when project creation, we just need to configure it to match your MongoDB server configuration. Open and edit `grails-app/conf/application.yml` and you will find at the bottom of the file the MongoDB configuration like below.

---
grails:
    mongodb:
        host: localhost
        port: 27017
        #username: ""
        #password: ""
        #databaseName: "mydatabase"

Uncomment the username, password and database name with your MongoDB server configuration. We are using MongoDB on localhost, so it looks like this.

---
grails:
    mongodb:
        host: localhost
        port: 27017
        username: ""
        password: ""
        databaseName: "bulletinboard"

Now, run your MongoDB server on the other terminal or command line by type this command if there's not running yet.

mongod


3. Create a Domain Class

To save data to MongoDB collection or table we have to create a domain class first. For that, in the Grails interactive console type this command.

create-domain-class grails.rest.Board

Next, open and edit `grails-app/domain/grails/rest/Board` then replace all codes with this.

package grails.rest

import grails.databinding.BindingFormat

class Board {

  String title
  String description
  String location
  @BindingFormat('dd/MM/yy HH:mm')
  Date eventDate

  static constraints = {
  }
}

As you can see above, we will receive event date fields as a string with the format "dd/MM/yy HH:mm" and it will binding as Groovy Date using `@BindingFormat` annotation.


4. Create a RESTful Controller

This time for creating a RESTful controller for CRUD function. In the Grails interactive console type this command.

create-restful-controller grails.rest.Board

That command will create a RESTful controller of Board domain class that contains this codes.

package grails.rest

import grails.rest.*
import grails.converters.*

class BoardController extends RestfulController {
    static responseFormats = ['json', 'xml']
    BoardController() {
        super(Board)
    }
}

This means the controller is ready for use as create-read-update-delete (CRUD) function.


5. Run and Test RESTful API CRUD Application

We will test the RESTful API using `CURL` from terminal or command line. To get all board list data, type this command in the terminal.

curl -i -H "Accept: application/json" localhost:8080/board

That command will display a response an array of boards like this if the data already exists.

HTTP/1.1 200
X-Application-Context: application:development
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 28 Jan 2018 11:58:52 GMT

[{"id":1,"title":"Morning Brief","eventDate":"2018-01-29T01:30:00Z","location":"school yard","description":"Usual morning brief for all students and schools employee"}]

To get a single board data, type this command.

curl -i -H "Accept: application/json" localhost:8080/board/1

Then a single board data will be displayed in the terminal.

HTTP/1.1 200
X-Application-Context: application:development
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 28 Jan 2018 12:01:30 GMT

{"id":1,"title":"Morning Brief","eventDate":"2018-01-29T01:30:00Z","location":"school yard","description":"Usual morning brief for all students and schools employee"}

To post a board to RESTful API, type this command.

curl -i -X POST -H "Content-Type: application/json" -d '{"title":"Annual Holiday","description":"Annual holiday for all students","location":"none","eventDate":"01/02/18 00:00"}' localhost:8080/board

The response below will be displayed if posting successfully.

HTTP/1.1 201
X-Application-Context: application:development
Location: http://localhost:8080/board/show/2
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 28 Jan 2018 12:26:19 GMT

{"id":2,"title":"Annual Holiday","eventDate":"2018-01-31T17:00:00Z","location":"none","description":"Annual holiday for all students"}

To update current or specific data by ID type this command for example just edit a title.

curl -i -X PUT -H "Content-Type: application/json" -d '{"title":"Independence Day Holiday"}' localhost:8080/board/2

You will see this response if update successful.

HTTP/1.1 200
X-Application-Context: application:development
Location: http://localhost:8080/board/show/2
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 28 Jan 2018 12:28:33 GMT

{"id":2,"title":"Independence Day Holiday","eventDate":"2018-01-31T17:00:00Z","location":"none","description":"Annual holiday for all students"}

To delete a data by ID type this command.

curl -i -X DELETE localhost:8080/board/2

If data deleted successfully, you will see this response.

HTTP/1.1 204
X-Application-Context: application:development
Date: Sun, 28 Jan 2018 12:29:55 GMT

That's it, the easy way to create a RESTful API using Grails 3, MongoDB and REST-API Profile. You can compare this easy way with the previous tutorial of creating RESTful API and using other frameworks like following:

- Step by Step Tutorial Grails 3 REST Web Service
- Tutorial of Building Java REST API using Spring Boot and MongoDB
- How to Create REST API Easily using Node.js, Express.js, Mongoose.js, and MongoDB

You can find the working source code on our GitHub.

Thanks!

The following resources might be useful for you:

Loading…