Spring Boot: Starting up with basic microservice

Welcome back guys,

Today i discovered a new branch of spring framework called “Spring Boot”. http://projects.spring.io/spring-boot/

It provides easy way to create stand alone, production ready app with minimal configuration, i can say Zeeeeero configuration.

And yes, boot provides a easy way to create Microservices System. Microservice means divide a large scale project/application into small deployable components. This is the simple definition.

even STS (Spring Tool Suite) makes our journey very very easy in the way of development.

Features

Here is the features of Spring Boot which is copied from official site of Spring Boot, :).

  •     Create stand-alone Spring applications
  •     Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  •     Provide opinionated ‘starter’ POMs to simplify your Maven configuration
  •     Automatically configure Spring whenever possible
  •     Provide production-ready features such as metrics, health checks and externalized configuration
  •     Absolutely no code generation and no requirement for XML configuration

Better to go for demo instead of katha (too much in detail), you will feel the beauty of boot.

Prerequisites

  • Spring Framework 4.1.5+
  • Maven 3.2+ (Better to have, Gradle otherwise)
  • STS – Spring Tool Suite (Preferred)/Eclipse
  • Java 7(Definitely :))

Demo

To create new project
in STS -> File -> Select Spring Starter Project

It will provide a wizard to configure Application
First step will allow to configure below attributes
Name: Name of the application
Type: Can be either Maven or Gradle.
Packaging: Can be Jar or War.
Java Version: Shows installed java versions.
Language: Language in which you want to for
And add Build specific attributes like Group, Artifact, Version, Description, Package.

Hit Next.

Here you will get simplicity of STS for Booting an application.
Boot will allow to select modules from this screen, all modules are grouped by category.

Once you select any modules from here spring will automatically add dependency in project (pom.xml in case of maven) and also intellisense acts as per your dependency and suggest you to change property for that particular dependency/modules.

Hit Next.

Here STS shows the URL which is created based on your selection from previous screens. STS is using this url to download a project with specified configuration and open it in editor.

you can see how it configures in url.

http://start.spring.io/starter.zip
?name=my-first-boot-app
&groupId=com.kode12
&artifactId=my-first-boot-app
&version=0.0.1-SNAPSHOT
&description=my-first-boot-app
&packageName=com.kode12.myfirstbootapp
&type=maven-project
&packaging=jar
&javaVersion=1.8
&language=java
&bootVersion=1.3.3.RELEASE
&dependencies=web

Spring allows us to create a same boot application using web interface also. The screen below shows same in web.
Web Url: https://start.spring.io/

Click on Switch to the full version link to open a full UI same as STS dependency selection screen.

Here we selected Web from category Web as we just want to try for basics.
On click of Finish STS will download pre-configured project and open it for you in editor.

On very first look spring created 4 application specific files for us

  • MyFirstBootApplication.java
  • MyFirstBootApplicationTests.java
  • pom.xml
  • application.properties

and maven downloads declared dependency in pom file.
Now, we will see a each files in detail.

pom.xml

Code

Here, i placed pom which is created by STS only, which shows
spring-boot-starter-parent artifactId with boot 1.3.3.RELEASE version, this would be used to get other dependency.

2 more dependency were added by default: spring-boot-starter-web to provide web support and spring-boot-starter-test to provide testing feature.

one more build plugins are added to provide build for Boot app with atrifactId spring-boot-maven-plugin.

MyFirstBootApplication.java

Code

2 thing you need to understand in this class

  • @SpringBootApplication annotation
  • main(...) method

main(…) method: Every boot application contains one class which contains main(…) method. the beauty and simplicity of this application is we can  run application by simply executing main method only. boot use embeded tomcat to run the application, no external servlet container/application server required.

@SpringBootApplication annotation: As we used some annotation like @Configuration, @ComponentScan etc while configuring Spring App, here Boot framework created one annotation which internally use some annotation which fulfill basic requirement of Boot app. Screen below shows annotation covered under  @SpringBootApplication annotation.

MyFirstBootAppApplicationTests.java

Code

Boot create a blank test for us based on JUnit test case. currently we are not focusing on it, will see something on upcoming posts.

application.properties

By default this file remains empty, we can add required configuration using this files.

for example: i want my server to be run on port 8081 so we need to set predefined property in this file called server.port. in that case my application.properties file would be

server.port=8081

that’s it !. now when i execute main method to start my embedded server, it will run on http 8081 port.

Boot will provide all the predefined properties of selected dependency and some basic properties like server.port etc.

Create a Hello Controller
Now, in next step lets create one controller to test a boot app. i created HelloController under controller package shown in image below.

Code

If you are experienced with spring mvc than it’s easy to understand configuration of this class. @RestController is annotation to inform engine that all methods for this class should return non-view content, same as we need to use @ResponseBody with method and @Controller with class.
Noooooowwwww time arrive to run the application, lets do it.

Run an application

To run an application you just need to invoke main method from MyFirstBootAppApplication.java class, or use Run As -> Spring Boot App (Atl + Shift + X, B) from STS.

It will start embedded tomcat container with http port 8081 (as we configured in application.properties file), see console log for more detail.

Console log

Test an App

to test a webservice hit the urls in your favourite browser.

Url: http://localhost:8081/hello/
Output:

 

Another url: http://localhost:8081/hello/again
Output:

 

Hope, you will get some basic idea about spring boot and microservices.
please feel free to comment for any suggestions.

Share current post by copy: http://goo.gl/BU7VA9

:)