Spring Boot Hello World project

What is Spring Boot ?

Spring Boot is an awesome framework which was created by Pivotal team to built Spring based applications. I assume you've already created at least one spring based application in older approach. Unless, try this Spring MVC hello world project  to understand about it before learning Spring Boot. 

Spring Boot can be used vastly to create Microservices. Then you need to have an idea about Microservices. Before you learn about Microservices, it is better to understand about SOA (Service Oriented Architecture).

What is a SOA (Service Oriented Architecture) ?

  • Service Oriented Architecture is a architectural design.
  • It makes your project into several loosely coupled services. 
  • It make your code easier to develop, run and test. Because it makes a huge code into separate layers.

What is a Microservice ?

  • Microservice is a kind of Service Oriented Architecture. 
  • As to Pivotal they define it as "Loosely coupled service oriented architecture with bounded context".
  • It improves resilience and component re-usability. 
  • Actually microservices are not simple things to implement and it should be done in well organized way.
  • It takes high cost and it is really depend on your company and the project needs which you are going to develop.
  • Look at the following image to identify the difference between Monolith vs Microservices 
Image credit : https://dzone.com/articles/comparing-microservices-and-monolithic-application
Read more about Monolithic vs Microservices

Why Spring Boot ?

  • Spring Boot has developed to achieve loosely coupling concept.
  • Spring Boot acts as a template which can be used to implement Spring applications easily. 
  • As you know, Spring framework is based on loosely coupling. Microservices are also used to create applications with loosely coupling concept. 
  • Now you will be able to understand what is the link between Microservices and Spring Boot. 

If you familiar with Spring framework, it will be  very easy to work with Spring Boot because Spring Boot can simplify whatever the thing that you develop with pure Spring. 

Why does it say Spring Boot is easy ?

  • Easy dependency management
Once you create Spring Starter project, it will automatically download related dependencies according to your need and what you've selected.  

  • Embedded servlet container
Spring Boot comes with Apache Tomcat embedded server which is running on http://localhost:8080.  If you need any other servlet container, just you can exclude tomcat and add any other container that you need. Again if you want to change the listening port, you can change it on application.properties file very easily.
  • Automatic configurations
If you are familiar with Spring MVC, I think you can remember dispatcher-servlet.xml, application-context.xml...etc. In Spring Boot, those configurations are automatically done by itself. You don't need to worry about it.

  • Easy configuration management
Spring Boot is very powerful with application.properties file.

  • Spring Boot Dev tools

Spring Boot Hello world

Step 01

Create a new Spring Starter project. Look at the fields that I've filled. You can choose build type, package type, Java version...etc. Otherwise you can create you project online with https://start.spring.io/

Step 02

Once you've clicked Next, following configuration window will be appeared. You can choose whatever the dependencies that you need according to your project. It is really easy to insert dependencies than older way. I've selected web. It automatically download the related dependencies into your project.

Step 03

Finally your project structure will be look like this. 

Step 04

I created a controller class called "RestController.java" in a separate package and created just a simple method called "showHello()".

package com.app.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;

public class RestController {

 public String showHello(){
  return "Hello world";

I think you are familiar with @Controller annotation. In here I've used @RestController annotation. This makes a class as a controller and methods in this class will return only domain objects instead of views. Then you don't need to worry about view. Just you can run this.

I have used @GetMapping annotation too. This can be used instead of using @RequestMapping (method=RequestMethod.GET, value="/hello"). Both are same. You can use @GetMapping to GET requests and @PostMapping to POST requests.  

Step 05

I've mentioned String Boot is packed with embedded server. You can use it very easily. Right click on the project, go to Run As and then select Spring Boot project.

Once server is started you can run your simple Hello world application as follows.


How easy is this. You need not to create server instances, adding projects, build projects...etc. Just you can run it. 

Look at the following server log.

Sometimes you will get an error with server port. you can check listening server ports by hitting netstat -ao on command prompt. It will show Local address, port , status and PID(Process Identification Number). You can terminate current listening port or you can change the embedded server port using application.properties file.
  1. If you want to terminate the current process of 8080 port, just you can hit netstat -ao and then identify the PID of related listening port. Then you can hit taskkill /F /PID {PID number} For example "taskkill \F \PID 10920". Then you can run your application again. 
  2. Other way, you can simply change the embedded server port. In you application.properties file, add server.port=8083 line. Then it will run on 8083 port. This is very simple. As I mentioned above, Spring Boot provides a lot of simple configurations in application.properties file. 
Or else you can kill the java process using Task Manager.

This is a simple demonstration of hello world application in Spring Boot. There are some other ways to do the same.

Advantages of Spring Boot

  • Spring Boot is simple.
  • Automatic configurations and easy to change properties.
  • It can be created runnable jars with embedded tomcat. (mvn spring-boot:run)
  • Very easy dependency management.
  • Very good solution to create Micro services.
  • Strong support for related Spring applications.
  • Spring Boot Dev Tools provides a bunch of advantages.
  • Good production support (Health checking, jvm metrics...etc)

Disadvantages of Spring Boot

  • Even though Spring Boot is simple, you need to know the concepts of Spring.   

Spring Boot Hello World project Spring Boot Hello World project Reviewed by Ravi Yasas on 3:04 PM Rating: 5

No comments:

Powered by Blogger.