Getting Started with Spring Boot: A Beginner’s Guide

Mutahir Manzoor
4 min readSep 3, 2023

--

Spring Boot is a powerful and popular framework for building Java applications. It facilitates the development process by providing a set of standards and defaults, which allows developers to quickly create production-ready applications. Whether you’re a experienced Java programmer or just getting started with programming, Spring Boot can simplify your life. In this article, we’ll walk you through the basics of getting started with Spring Boot.

What is Spring Boot?

Spring Boot is an extension of the Spring Framework, which is widely used in Java application development. While the Spring Framework provides a comprehensive set of tools for building enterprise-level applications, it can be quite complex to set up and configure. This is where Spring Boot comes into play. Spring Boot aims to simplify the development process by providing a set of pre-configured templates, dependencies, and conventions.

Here are some key features of Spring Boot:

  1. Auto-Configuration: Spring Boot automatically configures many aspects of your application based on the dependencies you include. This reduces the need for manual configuration.
  2. Standalone: Spring Boot applications can be run as standalone JAR files, which makes deployment and distribution easy.
  3. Production-Ready: Spring Boot includes built-in features for monitoring, health checks, and externalized configuration, making it suitable for production use out of the box.
  4. Prescriptive defaults: Spring Boot has Established defaults that boosts best practices, making it easier for developers to follow established patterns.
  5. Microservices: It’s well-suited for building microservices and can be combined with Spring Cloud for more advanced distributed systems.

Now you got the basic understanding of what Spring Boot is, you need to setup IDE.
Prerequisties:
Java: Spring Boot applications are written in Java, you must have Java Installed on your machine. Spring Boot is compatible with Java 8, 11, and 16 ,17.
Integrated Development Environment (IDE): While you can use any Java IDE of your choice, popular options include Eclipse, IntelliJ IDEA, and Visual Studio Code,IntelliJ IDEA.But one of my favorite is STS(spring tool suite).All these IDEs provide excellent support for Spring Boot development.
Maven or Gradle: Once your IDE is setup,next you need is build tool. Spring Boot supports both Maven and Gradle as build tools. You can choose the one that suits you.

Creating Your First Spring Boot Project

  1. Access Spring Initializer: Go to Spring Initializer in your web browser.The best thing about Spring initializer is user friendly GUI
  1. Let’s Configure Your Project:
  • Choose the build tool (Maven or Gradle).
  • Select the programming language (Java is recommended).
  • Set the project metadata (group, artifact, name, description, package).
  • Select packaging (JAR is commonly used).
  • Choose your Java version.

2. Add Dependencies:

  • Add essential dependencies like Spring Web.
  • Rest is up to you.Select the one that suits your need.

3. Generate and Download: Click “Generate” to create the project structure and download it as a ZIP file.

4. Import Project: Extract the ZIP file, open your IDE, and import the project as a Maven or Gradle project.

5. Go to Main Class : The main class will look like below

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MySpringBootApplication {

public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}

@SpringBootApplication annotation tells Spring Boot that this is the entry point for your application.

6. Write Your First Controller: Create a simple controller class that handles HTTP requests. Use annotations like @RestController and @RequestMapping to define your endpoints and methods.Your main class will look like this now.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class MySpringBootApplication {

public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}

7.Run Your Application: Run the main class (typically named Application) in your IDE or use terminal commands (./mvnw spring-boot:run for Maven, ./gradlew bootRun for Gradle).In STS right-click on your project select run as spring boot app.

8. Access Your App: Open a web browser and go to http://localhost:8080/hello to see your Spring Boot application in action and you will get “Hello, Spring Boot!”.

Congratulations! You have just created and run your first Spring Boot application.You can now start exploring more about Spring Boot and develop complex applications.

Thank you for joining us on this introductory guide to Spring Boot. Happy coding, and may your Spring Boot projects flourish!”

--

--