Hello World - Spring Boot
3 minutes read
If you are new to this post, this is a continuation of the springboot-series where I am attempting to develop an food delivery app with Spring Boot. Do check it out here.
Classical Hello World application using Spring Boot
Spring boot is one of the popular Spring based Java framework, which under the hood comprises of multiple projects at its core. Spring boot helps to get started with stand-alone web application applications with minimal coding.
Spring boot provides an Embedded Tomcat, Jetty or Undertow runtime environments for the jar/war to run as stand-alone applications along with many other useful features.
Web applications are developed at a faster phase using Spring boot owing to its inherent support for simplifying web development.
By end of this post you hopefully would have:
- Created a multi module maven Spring boot project
- Host a REST based API project which solves this
In addition to the above list few basic features are added which will be covered in separate post for each.
- OpenAPI Definition
- Spring Data JPA
- API Exception Handler
- Spring Boot Developer tools
These are the few features I deem to be minimal/essential when starting with API development.
Starting with the project structure.
Multi Module Maven Project
Multi module maven projects proved to be a successful project structure when working with teams varying from moderate to large in size. Having a parent pom.xml to control the version of the dependent libraries that are going to be common across multiple projects proved be successful. Instead of mentioning the same dependency in each of the child projects, with different versions, mentioning it once in the parent project proves to be manageable and less error prone. This comes in handy when you have to run your application for a SAST or DAST and have to bump up the version to address a particular CVE. It becomes painfully annoying when the application grows rapidly and we end up creating separate modules to handle growing business requirements (which we will be doing) and each having its own version for the same dependency.
The project structure which I will be using is,
With the multi-module maven setup the parent project is annam-services which contains multiple modules. For now we have one module named core. We can see the benefits of this project structure in the pom.xml files below.
The parent pom.xml to the left holds all the major dependencies while the child pom.xml need doesn’t need to repeat it every time.
The basic dependencies that are present in parent module are,
<dependencies>
<!-- Main Spring boot web dependency-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Developer friendly tools-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.0</version>
</dependency>
<!-- Spring Data with Postgres-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Bootstrapped single dependency for swagger and swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- Spring boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Spring boot starter web acts as the parent for our parent module,
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
The rest of the meta information for the parent module is,
<groupId>io.annam</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>parent</name>
<description>Parent Project for Annam Services</description>
<packaging>pom</packaging>
The key property is the tag where the value is set to pom
.
Having outlined few of the benefits of a multi-module maven project let’s see about creating endpoints in the next post .
java springboot-series springboot
20, Mar 2021