Spring Boot: The Game Changer for Rapid Application Development

Spring Boot: The Game Changer for Rapid Application Development

Introduction

In this article we'll look at a big picture of how Spring Boot helps to quickly build production ready applications. Setting up Spring projects earlier was not easy! It was required to manage dependency injection through xml files, and implement non-functional requirements such as error handling, log management and monitoring. Hence, to build production ready applications it would take few days to just setup the project and of course countless hours of maintenance.

Setting up a Spring Boot project

Spring Initializr is one of the ways to create a Spring Boot application. It provides a set of options to get started with, for example - project : Maven/Gradle, language: Java/Kotlin, Spring Boot version etc. After selecting the options of your choice a zip that contains project structure and build file can be downloaded. The unzipped project is then used in an IDE to start developing your project.

Spring Initializr does not generate code for you, it generates the project for you as per your selections and dependencies you added.

Note: For using Spring Boot 3, Java 17 and above is required.

Build Tools

Spring Boot supports both Maven and Gradle as build tools. In Maven, an xml formatted file named pom.xml is created with required dependencies and plug-ins. Maven’s rigid approach keeps things consistent from project to project and environment to environment.

All the dependencies that are available in Maven Dependencies directory are a result of above starter projects mentioned in pom.xml

Hello World REST API

  1. Define a class with @SpringBootApplication and main method inside it will serve as entry point.

     package com.example.demo;
    
     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;
    
     @SpringBootApplication
     public class SpringBootDemo {    
         public static void main(String[] args) {
             SpringApplication.run(SpringBootDemo.class, args);
         }
     }
    
  2. Create a class Movie with member variables, getters and toString method.

     package com.example.demo;
    
     public class Movie {
    
         private String id;
         private String name;
         private double rating;
    
         public Movie(String id, String name, double rating) {
             this.id = id;
             this.name = name;
             this.rating = rating;
         }
    
         public String getId() {
             return id;
         }
    
         public String getName() {
             return name;
         }
    
         public double getRating() {
             return rating;
         }
    
         @Override
         public String toString() {
             return "Movie [id=" + id + ", name=" + name + ", rating=" + rating + "]";
         }
    
     }
    

Note: If you don't generate getters and toString methods in class movie, following error will be displayed in server logs because object mapping will not be possible otherwise.

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.example.demo.Movie and no properties discovered to create BeanSerializer

  1. Define a Rest Controller with endpoint "/movies" to retrieve a list of movies.

     package com.example.demo;
    
     import java.util.Arrays;
     import java.util.List;
    
     import org.springframework.web.bind.annotation.RequestMapping;
     import org.springframework.web.bind.annotation.RestController;
    
     @RestController
     public class MoviesController {
    
         @RequestMapping("/movies")
         public List<Movie> getAllMovies() {
             return Arrays.asList(
                     new Movie("1CE23", "La La Land", 8.1),
                     new Movie("14DL3", "Spider Man", 8.2),
                     new Movie("87FG9", "Jumanji", 7.4));
         }
     }
    

Output

The SpringBootDemo application can be accessed via localhost:8080/movies on browser of your choice.

[
  {
    "id": "1CE23",
    "name": "La La Land",
    "rating": 8.1
  },
  {
    "id": "14DL3",
    "name": "Spider Man",
    "rating": 8.2
  },
  {
    "id": "87FG9",
    "name": "Jumanji",
    "rating": 7.4
  }
]

Conclusion

As it has been demonstrated with above example, creating REST API with Spring Boot is very easy. It wasn't required for us to configure any Spring Beans or xml. However, there is a lot that happens in the background. In the upcoming articles, we'll explore how Spring Boot does it for us with minimal developer intervention.