• Join StackChief
  • Blog
  • Tutorials
  • Questions
  • React
  • JavaScript
  • MongoDB
  • NodeJs
  • Kafka
  • Java
  • Spring Boot
  • Examples

Tutorials

Spring Boot Tutorial

This is a quickstart tutorial for Spring Boot. We'll build a simple REST API using Spring Boot and Maven.

Key Takeaways

  • Understand the basic structure of a Spring Boot project.
  • Set up Spring Boot dependencies and plugins using Maven.
  • Create a REST controller with Spring Boot.
  • Build and run your Spring Boot application locally.

Spring Boot Tutorial: Getting Started

First, ensure you have Maven installed. If not, download the latest stable version from the official site. Start by creating a new directory and navigate into it. Use the following command to initiate your Spring Boot application structure:

mkdir -p src/main/java/com/example

This command sets up the basic project directory structure. Next, add your pom.xml to the project's root directory. This crucial file handles the Maven build configuration, specifying dependencies and plugins. Below is a modernized version of the pom.xml:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>hello-spring-boot</artifactId>
    <version>1.0.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <java.version>17</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

This configuration adds the current Spring Boot dependencies and the Spring Boot Maven plugin.

Creating The REST Controller

To create a sample REST endpoint, we'll add a Java class with Spring Boot REST controller annotations. Under the com/example directory, add a WebAppController.java.

WebAppController.java

package com.example;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebAppController {

    @GetMapping("/")
    public String mainPage() {
        return "Hello World";
   }

}

This defines a basic REST endpoint at the / path, returning "Hello World" to the client.

Creating Spring Boot App

In the same directory, create another Java class, Application.java.

Application.java

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}

This class serves as the entry point for launching the application.

Running the App

Back at your project's root, build the project using Maven:

mvn clean package

Once the build completes, run the application:

java -jar target/hello-spring-boot-1.0.0.jar

Your application will start, typically displaying a message with the local port number. Use a web browser to visit http://localhost:8080, where "Hello World" should appear.

Conclusion

You've successfully created and run a basic Spring Boot REST API. To upgrade your return data, consider using template engines like Thymeleaf to render dynamic web pages seamlessly.

FAQ

Do I need a specific Java version for Spring Boot?

Spring Boot recommends using Java 17 or later for optimal compatibility and performance as of recent releases.

Can I use Gradle instead of Maven?

Yes, Spring Boot supports both Maven and Gradle. The choice depends on your preference and familiarity with the build tool.

How do I add more endpoints to my Spring Boot application?

Simply create additional classes or methods annotated with @GetMapping, @PostMapping, and other relevant Spring annotations to define new endpoints.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews
Comment