How to Setup Environment for Spring Boot 3

Spring Boot is a framework that simplifies the development and deployment of Spring-based applications. Spring Boot 3 is the latest major release that brings many new features and improvements, such as Java 17 support, native image support, improved configuration properties, and more.

In this blog, we will learn how to setup the environment for Spring Boot 3 development, using Maven as the build tool and IntelliJ IDEA as the IDE. We will also create a simple web application with Spring Boot 3 and run it locally.

Prerequisites

  • Java 17 or later installed on your system.
  • Maven 3.5+ installed on your system.
  • IntelliJ IDEA installed on your system.

Create a Spring Boot 3 Project

The easiest way to create a Spring Boot 3 project is to use the Spring Initializr web tool. Spring Initializr allows you to generate a customized project with the dependencies and settings you need.

To create a Spring Boot 3 project with Spring Initializr, follow these steps:

  • Go to Spring Initializr website
  • Select Maven Project as the project type, java as the language, and 3.0.0+ as the Spring boot version (Do not choose SNAPSHOT version)
  • Enter the group, artifact, name, and description of your project. For example, com.example as the group, demo as the artifact, spring-boot-3-demo as the name, and Demo project for Spring Boot 3 as the description.
  • Select the dependencies you want to add to your project. For this blog, we will only add the Spring Web dependency, which provides the web development features for Spring Boot.
  • Click on the Generate button to download the project as a zip file.
  • Extract the zip file to your preferred location.

You have now created a Spring Boot 3 project with Maven and Spring Web dependency.

Import the Project into IntelliJ IDEA

To import the project into IntelliJ IDEA, follow these steps:

  • Open IntelliJ IDEA and select File -> Open.
  • Navigate to the folder where you extracted the project and select the pom.xml file.
  • Click on the Open as Project button to import the project as a Maven project.
  • Wait for IntelliJ IDEA to sync the project and download the dependencies.

You have now imported the project into IntelliJ IDEA and ready to start coding.

Create a Simple Web Application

To create a simple web application with Spring Boot 3

  • Open the src/main/java/com/example/demo/DemoApplication.java file, which is the main class of the project. This class is annotated with @SpringBootApplication, which enables the auto-configuration and component scanning features of Spring Boot.
  • Create a new class called FirstController in the same package as the main class. This class will be a web controller that handles the HTTP requests to the /hello endpoint. Annotate this class with @RestController, which indicates that this class is a web controller that returns the data as JSON. Also, import the org.springframework.web.bind.annotation.* package.
  • Create a method called hello in the FirstController class. This method will return a simple greeting message as a string. Annotate this method with @GetMapping(“/hello”), which indicates that this method handles the GET requests to the /hello endpoint. Also, add a parameter called name of type String to this method. Annotate this parameter with @RequestParam, which indicates that this parameter is a query parameter in the request URL. For example, http://localhost:8080/hello?name=John. Set the default value of this parameter to World using the defaultValue attribute of the @RequestParam annotation.

The code of the HelloController class should look like this:

package com.example.demo;

import org.springframework.web.bind.annotation.*;

@RestController
public class FirstController {

    @GetMapping("/hello")
    public String hello(@RequestParam(defaultValue = "World") String name) {
        return "Hello, " + name + "!";
    }
}

You have now created a simple web controller that returns a greeting message based on the query parameter.

Run the Application Locally

To run the application locally,

  • Right-click on the DemoApplication class in the project explorer and select Run ‘DemoApplication’. This will start the Spring Boot application on the default port 8080.
  • Open your browser and go to http://localhost:8080/hello. You should see the message Hello, World! on the screen.
  • Try changing the query parameter to your name, such as [http://localhost:8080/hello?name=John]. You should see the message Hello, John! on the screen.
  • You have now run the application locally and tested the web controller.

In this blog, we have learned how to setup the environment for Spring Boot 3 development, using Maven and IntelliJ IDEA. We have also created a simple web application with Spring Boot 3 and run it locally. We have seen how Spring Boot 3 simplifies the configuration and development of Spring-based applications.

We hope this blog has been helpful and informative for you. Thank you for reading and happy coding!

Referances

  • https://www.baeldung.com/spring-boot-properties-env-variables
  • https://spring.io/guides/gs/spring-boot/
  • https://dev.to/lohrran/spring-boot-development-environment-37d7
  • https://github.com/spring-guides/gs-spring-boot.git

Leave a Reply