Spring Boot Application

Learn via video courses
Topics Covered

Overview

Spring framework is a popular open-source framework for creating enterprise production-grade applications.

Over the years, the spring framework has become the default choice for building enterprise applications. Spring framework provides dependency injection and easy integration with different technologies RDBMS, NoSQL, Middleware, etc.

Introduction to Spring Boot Application

The definition of Spring Boot from the official website is as follows:

`Spring Boot helps you to create stand-alone, production-grade Spring-based Applications that you can just run.

Introduction to spring boot application Spring framework was capable enough to build any application, but that still required time and knowledge to configure, build and deploy the application. As simple as exposing a rest endpoint requires lots of manual configuration.

Spring boot takes the simplification one step ahead and allows the application developer to focus on business logic from day one.

Features of Spring Boot

  • Creates stand-alone production-grade applications.
  • Has embedded web servers (no need to deploy WAR files).
  • Provides ‘starter’ dependencies to simplify our build configuration.
  • Provides production-ready features such as metrics and health checks.
  • Allows` auto-configuration based on available JAR.

Spring Boot Hello World Application

Let's build a simple hello world application using spring boot. The application will expose just one rest endpoint that will return the response "Hello World!".

Pivotal web service provides us with tools to generate skeleton projects within a few clicks. Spring Initializer is one such tool.

Step 1 - Spring Initializer

We will use start.spring.io to create a “web” project. Select the below options from the page.

  • Project - Maven project
  • Language - Java
  • Version - 2.7.2(Might change in future with new releases)
  • Group id - com.scaler.spring-boot
  • Artifact id - helloworld
  • Add Dependency - Search for "web" and add it.
  • Packaging - Jar
  • Java version - 11 preferred, but you can choose the version that matches your system.

You can navigate to the same settings from here.

Hit the “Generate” button, download the zip, and unpack it into a folder on our computer.

Step 2 - Import the Project Into Your IDE

Import the application into the IDE of our choice. For IntelliJ IDEA, go to the “File” menu, locate the Pom.xml file, and click “Open a a project.

Step 3 - Create Rest Controller

Create a rest controller which exposes endpoint /hello and returns the response "Hello world!"

Step 3 - Run the Application

From your IDE, locate the class HelloWorldApplication and just run it.

Step 4 - Verify

By default, the spring boot application runs on tomcat on port 8080. Invoke the endpoint http://localhost:8080/hello should return the desired response.

Build Spring Boot Application Using maven

The POM of the download application should contain the below dependency.

  • spring-boot-starter-parent - Parent pom for all the spring boot projects.
  • spring-boot- starter-web - Provides support for the web
  • spring-boot-starter-test - Provides support for unit testing.
  • spring-boot-maven-plugin - Bundles and packages artifacts into a bootable JAR.

To build the application, just run the maven command mvn clean install. It will create fat runnable JAR.

Once you have the fat JAR, it can run on any system where Java virtual machine is available using the command Java –jar <SpringBootAppName.jar>.

Build Spring Boot Application Using Gradle

To generate Gradle based project, choose "Gradle project" from start.spring.io. For Gradle-based projects, dependency will remain the same but the command to build Gradle is specific.

Use the command grade build to create the jar and run it similarly.

Conclusion

  • Spring boot simplifies application development and takes care of heavy lifting itself.
  • Spring boot allows developers to focus on business from day one without worrying about different integration points.
  • Use start.spring.io to generate the skeleton project.
  • Spring boot generates a bootable jar file, which can run in any system where JVM is available.