Showing posts with label Rapid-development. Show all posts
Showing posts with label Rapid-development. Show all posts

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:

Wednesday, July 04, 2007

A True Framework

Java enterprise software development has become more comlex and unpleasant than ever with increasing stack of technologies added; fast, complex and ever changing specs coming out of JCP; more steap learning curve from version to version with increased complexity than simplicity; lack of good tooling support; and on top of all these more and more vendor specific extensions to know for making any project successful.

Spring framework has been gaining support among the Java community which is trying to simplify (and in many ways has succeeded) Java enterprise software development. Spring is a light-weight modular stack of Java frameworks. At its core is a light-weight POJO based IOC container that brings in a powerful design pattern - Inversion Of Control (IOC) or Dependancy Injection(DI) to Java applications. It is more nice for anybody to say that one is developing a Spring-based Java enterprise applicaton than to say J2EE-based (rather JavaEE-based) application.

Recently, Ruby On Rails (RoR) has been gaining popularity both inside and outside of Java community as an attractive alternative to Java-based web application complexities. In many ways RoR has clearly shown and proven to the developer community, what a "true framework" should look like. Even very mature enterprise technologies like Java with a wide variety of frameworks available for its community is shamelssly learning from this fast growing baby - RoR.

In my opinion "a true framework" should promote, automate and even force several best practices and make the development fast but simple, pleasant and more enjoyable.

RoR – a very well designed framework is based on THREE principles:
  • DRY – Do not Repeat Yourself
  • Convention over Configuration - which eliminates lot of configuration headaches
  • Code Generation – eliminates the need for repeated startup code

I was initially impressed by the simplicity and elegance of Java language when I first moved from C++ in 1999. It has been quite enjoyable developing Java applications. I have been trying to keep myself abreast with the latest and greatest things happening in Java community. Since then I never had to look back up until I have come across RoR that made me to think like "Java development is tedious and complex". Open source community has contributed an amazing list of quality frameworks to Java. Even with good open source frameworks like Jakarta, Struts, HIBERNATE, Spring etc., developing an enterprise Java application is still tedious and complex. Java development community deserves better frameworks for rapid development and truly making Java development a pleasant experience.

After learning and experiencing RoR, I started looking for such frameworks or initiatives in Java community. Though there is NONE currently available that is even close to RoR, I have come across a few initiatives that are happening along similar lines.

Following is a shot list of some of such initiatives for Rapid Java Application Development:

  • AppFuse - seems promising. Try AppFuse light/ Equinox and Jetty for even more rapid results.
  • Sculptor - a model driven development Java application framework
  • AndroMDA - another model driven development Java application framework.
  • Grails - a RoR like framework for Java web-based development.

Finally, it is a good sign to see that Java is heading towards simplicity with several open source frameworks and initiatives shifting developer community from traditional Java Enterprise Development towards simple more agile way - for a fast, better and pleasant experience. :)