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 some guides from Grails Guides. Previously, we have shown you how to create Grails 3 REST API using a 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.
Table of Contents:
- Create a new Grails 3 Application with REST-API Profile
- Configure Grails MongoDB plugin
- Create a Grails Domain Class
- Create a Grails RESTful Controller
- Run and Test Grails MongoDB REST API CRUD App
The following tools, frameworks, and library are required for this tutorial:
Please make sure that you already download and install Grails 3.3.2 and MongoDB before continue to the next steps.
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.
Configure Grails 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
Create a Grails 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.
Create a Grails 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 these 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.
Run and Test Grails MongoDB REST API CRUD App
We will test the REST API using `CURL` from the 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-board to REST 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 REST API using Grails 3, MongoDB and REST-API Profile. You can compare this easy way with the previous tutorial of creating REST API and using other frameworks like the 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.
That just the basic. If you need more deep learning about Groovy and Grails you can take the following cheap course:
- Mastering Grails. A Comprehensive Grails Course.
- Groovy Scripting for Developers / Testers
- Introduction to JVM Languages Clojure, Kotlin, and Groovy
Thanks!