How do you create a Spring Boot application from scratch?

Table of Contents

Introduction

Spring Boot is a framework that simplifies the development of Spring applications by providing production-ready features like embedded servers, auto-configuration, and a comprehensive set of libraries and tools. It allows you to quickly create stand-alone, production-grade Spring-based applications with minimal configuration.

In this guide, we’ll walk you through the process of creating a Spring Boot application from scratch, starting from setting up a project to running your first application.

1. Setting Up Your Development Environment

Before creating a Spring Boot application, ensure that you have the following installed on your system:

  • Java Development Kit (JDK): Spring Boot requires JDK 8 or later. You can download it from the official Oracle JDK website or use OpenJDK.
  • Integrated Development Environment (IDE): You can use any Java IDE such as IntelliJ IDEA, Eclipse, or Visual Studio Code.
  • Maven or Gradle: These are build tools used to manage dependencies and build the project. In this guide, we’ll use Maven, but you can use Gradle if you prefer.

2. Creating the Project Using Spring Initializr

The easiest way to create a Spring Boot project is by using Spring Initializr, a web-based tool that generates a Maven or Gradle project with the necessary dependencies.

Step 1: Go to Spring Initializr

Open your browser and navigate to https://start.spring.io.

Step 2: Configure the Project

On the Spring Initializr page, configure your project as follows:

  • Project: Choose Maven Project (or Gradle if you prefer).
  • Language: Select Java.
  • Spring Boot Version: Choose the latest stable version of Spring Boot.
  • Group: Enter the group ID (e.g., com.example).
  • Artifact: Enter the artifact ID (e.g., demo).
  • Name: Choose the name of your application (e.g., demo).
  • Description: Add a short description (e.g., "A simple Spring Boot application").
  • Package Name: You can use the default or specify a package name (e.g., com.example.demo).
  • Packaging: Choose Jar.
  • Java Version: Select 8 or later, based on your JDK version.

Step 3: Add Dependencies

Click on Add Dependencies and choose the dependencies you need for your application. For a simple web application, you might select:

  • Spring Web (provides RESTful web services and embedded web servers like Tomcat).
  • Spring Boot DevTools (for development-time tools like automatic restarts).
  • Thymeleaf (for server-side rendering if you plan to use templates).

Click Generate to download the project as a .zip file.

Step 4: Extract the Project

Once the .zip file is downloaded, extract it to your desired folder. You can open the extracted folder in your IDE.

3. Exploring the Project Structure

When you open the project in your IDE, you’ll notice the following structure:

  • **DemoApplication.java**: This is the main class where the Spring Boot application starts. It contains the main method that calls SpringApplication.run().
  • **application.properties**: This file is where you can configure application-specific settings such as server ports, database configurations, logging, etc.
  • **pom.xml**: This file contains the Maven configuration, including dependencies for your Spring Boot project.

4. Writing Your First Spring Boot Application

Now that you have your project set up, let’s write the code for a simple Spring Boot application.

Step 1: Define the Main Class

Open DemoApplication.java and ensure it looks like this:

  • @SpringBootApplication is the main annotation that enables Spring Boot auto-configuration and component scanning.
  • The main() method runs the Spring Boot application by calling SpringApplication.run(), which sets up the application context and starts the embedded web server (Tomcat by default).

Step 2: Create a Controller

Now, let’s create a simple REST controller that responds to HTTP requests.

Create a new class, HelloController.java, in the com.example.demo package:

  • @RestController is a convenience annotation that combines @Controller and @ResponseBody, so you don’t need to annotate each method with @ResponseBody.
  • The @GetMapping("/hello") annotation maps HTTP GET requests for /hello to the sayHello() method.

Step 3: Run the Application

You can now run your Spring Boot application by executing the main() method in the DemoApplication class. Most IDEs like IntelliJ IDEA and Eclipse have built-in run buttons to launch the application.

Alternatively, from the command line, navigate to your project folder and run:

Spring Boot will automatically:

  • Start an embedded web server (Tomcat by default).
  • Scan your project for Spring components (e.g., the controller) and set up the necessary beans.

5. Accessing the Application

Once the application is running, open a browser and navigate to:

You should see the response: Hello, Spring Boot!

6. Customizing the Application

Modifying application.properties

You can customize your application using the application.properties file located under src/main/resources. For example, to change the default server port, add the following:

After restarting the application, it will run on port 8081 instead of the default 8080.

Adding More Endpoints

You can add more endpoints to your controller. For example:

Now, you can access this endpoint via http://localhost:8080/goodbye.

7. Packaging and Running the Application

To package your application as a JAR file, run:

This will create a target/demo-0.0.1-SNAPSHOT.jar file. You can run this JAR file using the following command:

Your Spring Boot application will start in the same way, with the embedded web server running on the specified port.

Conclusion

Creating a Spring Boot application from scratch is straightforward thanks to the ease of use provided by Spring Initializr and the power of Spring Boot's auto-configuration. You’ve learned how to:

  1. Set up a Spring Boot project using Spring Initializr.
  2. Understand the project structure and configure it.
  3. Create RESTful endpoints using a controller.
  4. Customize the application using application.properties.
  5. Run and package the application.

Spring Boot’s simplicity and flexibility make it an ideal choice for creating modern, production-ready applications with minimal effort.

Similar Questions