Sunday, May 15, 2016

Create a Secured Restful API App with Grails 3 and PostgreSQL - Step by Step: Part 1 of 5

Part 1: Create and run basic Grails RESTful app 

I was recently exploring Grails 3.1.6 rest-api profile to create a RESTFul application. I was able to get the base RESTful application developed with added security backed by PostgreSQL database JSON persistence at a rapid speed.

The path was bit bumpy but it helped me achieve what I wanted to, very fast. There were few explorations along the way and I share my experience in this 5 parts of my posts.

Environment: Grails 3.1.6, Java 1.8 on Mac OS X 10.9.5

Step1
Install Grails 3.1.6. I prefer SDKMAN
Use Grails 3.1.6 if you have multiple versions installed.
$ sdk install grails 3.1.6 $ sdk use grails 3.1.6 Using grails version 3.1.6 in this shell. $

Make sure that Grails 3.1.6 s the current version set.
$ grails -version | Grails Version: 3.1.6 | Groovy Version: 2.4.6 | JVM Version: 1.8.0_60 $

Step2
Check Available Profiles. I picked the highlighted rest-api profile for this application.
$ grails list-profiles | Available Profiles -------------------- * angular - A profile for creating applications using AngularJS * rest-api - Profile for Web API applications * base - The base profile extended by other profiles * plugin - Profile for plugins designed to work across all profiles * web - Profile for Web applications * web-plugin - Profile for Plugins designed for Web applications $

Step3
Create an application with rest-api profile
$grails create-app giri-api --profile=rest-api | Application created at /Users/Gpottepalem/temp/giri-api $

Step4
Simply run the application just created
$ cd giri-api $ grails run-app Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=2048m; support was removed in 8.0 BUILD SUCCESSFUL | Running application... Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=2048m; support was removed in 8.0 objc[41093]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined. Grails application running at http://localhost:8080 in environment: development

Step 5
Check the app, I used curl command line tool. Browser can also be used. The response is simply a JSON giving the details of the application, it's components and their versions.
$ curl http://localhost:8080 {"message":"Welcome to Grails!","environment":"development","appversion":"0.1","grailsversion":"3.1.6","appprofile":"rest-api","groovyversion":"2.4.6","jvmversion":"1.8.0_60","reloadingagentenabled":true,"artefacts":{"controllers":1,"domains":0,"services":1},"controllers":[{"name":"giri.api.ApplicationController","logicalPropertyName":"application"}],"plugins":[{"name":"eventBus","version":"3.1.6"},{"name":"jsonView","version":"1.0.9"},{"name":"i18n","version":"3.1.6"},{"name":"core","version":"3.1.6"},{"name":"dataBinding","version":"3.1.6"},{"name":"codecs","version":"3.1.6"},{"name":"restResponder","version":"3.1.6"},{"name":"controllers","version":"3.1.6"},{"name":"urlMappings","version":"3.1.6"},{"name":"dataSource","version":"3.1.6"},{"name":"domainClass","version":"3.1.6"},{"name":"hibernate","version":"5.0.5"},{"name":"interceptors","version":"3.1.6"},{"name":"mimeTypes","version":"3.1.6"},{"name":"controllersAsync","version":"3.1.6"},{"name":"services","version":"3.1.6"},{"name":"converters","version":"3.1.6"},{"name":"cache","version":"3.0.2"}]}

  Side Note
Grails 3 is built on top of Spring Boot. So, all of the boot Actuator RESTful end points are available.
Just to check a few, try the following (a browser can also be used):
$ curl http://localhost:8080/health $ curl http://localhost:8080/info $ curl http://localhost:8080/mappings $ curl http://localhost:8080/env $ curl http://localhost:8080/metrics $ curl http://localhost:8080/beans

Also, the grails command url-mappings-report is handy to check the REST end-points at various times during the development of the application.
From the project home directory run the following grails command, make sure that the application is stopped.
$ grails url-mappings-report | * | ERROR: 404 | View: /notFound | | * | ERROR: 500 | View: /error | | * | /${controller}/${action}?/${id}?(.${format)? | Action: (default action) | Controller: application | * | / | Action: index |

Now we have the basic RESTful application created and ready to be taken for a spin.

Just explore the project structure created by Grails. Much of it looks like a web application with some noticeable changes for rest-api. The main notable change is views directory under grails-app directory. Instead of .gsp files you will see .gson files. Grails 3.x added support for JSON views. Refer to Grails documentation for details.

In the next part, I will show steps to add security and secure RESTful end points.

Links: