How to create a Spring Boot application
According to the official Spring Boot website,
Spring Boot is a software that makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss.
Creating Your First Spring Boot Application
In this tutorial, we’ll guide you through the process of creating your first Spring Boot application.
Prerequisites
Before we begin, make sure you have the following:
- Java Development Kit (JDK) installed on your system.
- A code editor (e.g., IntelliJ IDEA, Eclipse, Visual Studio Code, etc.).
- Basic understanding of Java and Spring framework.
Step 1: Set Up a New Spring Boot Project
- Open your preferred Integrated Development Environment (IDE).
- Create a new Spring Boot project.
- You can use Spring Initializer (https://start.spring.io/) or your IDE’s built-in Spring Boot project creation tool as follows:
- Select the dependencies you need (e.g., Spring Web, Spring Boot DevTools, etc.).
Step 2: Create a Spring Boot Application Class
- Create a new Java class (e.g.,
MyApplication
) in thesrc/main/java/com/example/demo
directory. - Add the
@SpringBootApplication
annotation at the top of your class.@SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
Step 3: Create a Simple Controller
- Create a new Java class (e.g.,
HelloController
) in thesrc/main/java/com/example/demo
directory. - Add the
@RestController
and@RequestMapping
annotations.@RestController public class HelloController { @RequestMapping("/") public String hello() { return "Hello, World!"; } }
Step 4: Run Your Spring Boot Application
- Right-click on the MyApplication class and select “Run MyApplication”.
- Alternatively, you can run the application from the command line using Maven:
mvn spring-boot:run
Step 5: Access Your Application
Open a web browser and go to http://localhost:8080. You should see the message “Hello, World!“.