Coding

Spring Boot Tutorial for Beginners: Build Your First App

This Spring Boot tutorial for beginners builds a working web app in about ten minutes: generate a project with Spring Initializr, add one controller class, and start it with ./mvnw spring-boot:run or ./gradlew bootRun. Open http://localhost:8080/greeting and you get JSON back.

The steps follow the official "Building a RESTful Web Service" guide from Spring. Output shown below is example output, and the exact generated files can differ a little between Initializr versions.

What Spring Boot is, in one paragraph

Spring Boot is a layer on top of the Spring Framework that picks sensible defaults for you. You add a "starter" dependency, such as the web starter, and Boot configures the pieces that starter needs. For a web app that includes an embedded HTTP server, so there is no separate server to install and no deployment step just to try your code. The result is an ordinary Java program with a main method.

What you need first

  • A JDK. As of November 2026 the Spring Boot docs cover version 4.2.0, which requires at least Java 17 and works with versions up to and including Java 27. Check yours with java -version.
  • A build tool, but only through the wrapper. The generated project includes mvnw (Maven) or gradlew (Gradle), so you do not install Maven or Gradle yourself. If you do use your own, the docs list Maven 3.6.3 or later, and Gradle 8.14 or later in the 8.x line, or 9.x.
  • A text editor or IDE. Any works. IntelliJ IDEA, Eclipse and VS Code all open these projects.

Step 1: generate the project

Spring Initializr is the project generator that the Spring guides use. In the guide, the settings are:

  1. Choose Maven or Gradle, and Java as the language.
  2. Name the artifact rest-service.
  3. Add the Spring Web dependency.
  4. Click Generate, then unzip the downloaded file.

Spring Web is the starter that gives you Spring MVC, the JSON conversion library (Jackson) and the embedded server. The guide says you do not need to change the generated RestServiceApplication class. It is the entry point, and it carries the @SpringBootApplication annotation that turns on component scanning and auto-configuration for the package it sits in.

Step 2: add a data class

Create src/main/java/com/example/restservice/Greeting.java. A Java record is the shortest way to describe a JSON response:

package com.example.restservice;

public record Greeting(long id, String content) { }

Jackson turns an instance of this record into a JSON object with an id field and a content field. You write no conversion code.

Step 3: add a controller

In the same package, create GreetingController.java:

package com.example.restservice;

import java.util.concurrent.atomic.AtomicLong;

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

@RestController
public class GreetingController {

  private static final String template = "Hello, %s!";
  private final AtomicLong counter = new AtomicLong();

  @GetMapping("/greeting")
  public Greeting greeting(@RequestParam(defaultValue = "World") String name) {
    return new Greeting(counter.incrementAndGet(), template.formatted(name));
  }
}

Each annotation does one job:

  • @RestController marks the class as a web controller whose methods return data directly, rather than the name of a page to render.
  • @GetMapping("/greeting") sends HTTP GET requests for /greeting to this method.
  • @RequestParam(defaultValue = "World") reads the optional name query parameter and uses World when it is missing.

The controller must live in the same package as the application class, or in a sub-package of it. Component scanning starts from the application class's package, so a controller in an unrelated package is never found, and the URL returns 404.

Step 4: run it

From the project folder, start the app with the wrapper that matches your build tool:

./mvnw spring-boot:run
./gradlew bootRun

On Windows Command Prompt or PowerShell, use mvnw spring-boot:run or gradlew bootRun without the leading ./ (in PowerShell, .\mvnw). The first run downloads dependencies, so it takes longer. When the log shows the embedded server has started, visit the URL in a browser or with curl:

curl http://localhost:8080/greeting

Example output, as shown in the official guide:

{"id":1,"content":"Hello, World!"}

Now pass the parameter:

curl "http://localhost:8080/greeting?name=User"
{"id":2,"content":"Hello, User!"}

The id went from 1 to 2 because Boot creates one controller object and keeps it, so its counter survives between requests. Stop the app with Ctrl+C in the terminal.

You can also package it as a single runnable JAR. The guide uses ./mvnw clean package for Maven or ./gradlew build for Gradle, then java -jar with the file from target/ (Maven) or build/libs/ (Gradle).

Change the port

The embedded server listens on port 8080 by default. If something else already holds that port, you will see a startup failure; see how to find and free a port that is already in use. Otherwise, set the port in src/main/resources/application.properties:

server.port=9090

The Spring Boot docs say the same setting can come from the environment variable SERVER_PORT, and that server.port=0 makes the server pick a free port using the operating system. The docs also note that server.port=-1 switches off the HTTP endpoints while still creating the web application context, which is sometimes useful in tests.

Step 5: write a first test

The docs recommend @SpringBootTest for a test that loads your application context. By default it does not start a real server: the MOCK web environment is used, and requests go through MockMvc at the Spring MVC layer. This follows the pattern in the Spring Boot testing reference, applied to our endpoint (the generated project already has the test dependencies, but check your build file if an import does not resolve):

package com.example.restservice;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

@SpringBootTest
@AutoConfigureMockMvc
class GreetingControllerTests {

  @Test
  void greetsTheWorldByDefault(@Autowired MockMvc mvc) throws Exception {
    mvc.perform(get("/greeting"))
        .andExpect(status().isOk())
        .andExpect(content().string("{\"id\":1,\"content\":\"Hello, World!\"}"));
  }
}

Run it with ./mvnw test or ./gradlew test. The expected id is 1 because this is the first call to a fresh controller object in the test's application context. A second test in the same class that also calls the endpoint could see a higher number, so for real projects assert on the content field instead of the whole string. The docs also point out a limit: with the mock environment there is no real servlet container, so you cannot test things like a rendered custom error page. For that, use @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT), which starts the embedded server on a random port.

Common beginner problems

  • "Unsupported class file major version" or a Java version error. Your JDK is older than the project needs. Check java -version against the 17 minimum for current Boot, and make sure your IDE and terminal use the same JDK.
  • The page returns 404 for /greeting. The controller is probably outside the application class's package, or the method is missing @GetMapping.
  • The app stops right after starting. Without the web starter on the classpath there is no server to keep it alive, so it runs and exits. Check that Spring Web was added in Initializr.
  • Port 8080 is busy. Change server.port as shown above, or stop the other process.

Where to go next

Once this runs, add one thing at a time: a second endpoint that takes a path variable, a service class the controller calls, then a database through a data starter. Boot's starters keep each step small, because you add a dependency and the defaults come with it. If you are choosing between languages before committing to this stack, Java vs Python: the differences side by side covers how the two compare for beginners.

Frequently asked questions

Which Java version do I need for Spring Boot?
As of November 2026 the Spring Boot documentation covers version 4.2.0, which requires at least Java 17 and is compatible with versions up to and including Java 27. Run java -version to see what you have installed.
Do I need to install Tomcat to run a Spring Boot app?
No. The web starter brings an embedded server, so the application starts its own HTTP listener when you run it. By default it listens on port 8080.
Should I choose Maven or Gradle?
Either works. The official getting-started guide offers both, and the Initializr generates a wrapper script for whichever you pick, so you do not need to install the build tool yourself. Pick the one your team or tutorials use.
How do I run Spring Boot on a different port?
Set server.port in src/main/resources/application.properties, for example server.port=9090. The environment variable SERVER_PORT also works. Setting server.port=0 makes the server pick a free port.

Sources