Creating Your First Spring Application

Updated

The quickest reliable way to create a first Spring application is to generate a Spring Boot project at Spring Initializr, add Spring Web, put a controller under the application package, and run the Maven wrapper. By the end of this guide, GET /greeting returns a small JSON response.

Version assumptions (updated 7 August 2026): the example targets Spring Boot 4.1.x and Java 17 or later. Spring Boot 4.1 requires Java 17+, Maven 3.6.3+, or Gradle 8.14+/9.x. Let Initializr select the current compatible patch versions instead of copying dependency versions from an old tutorial.

1. Check Java

Run:

java -version

The reported major version must be at least 17. If java is missing or older, install a current JDK before continuing.

2. Generate the project

Open Spring Initializr and choose:

  • Project: Maven
  • Language: Java
  • Spring Boot: the current stable 4.1.x release
  • Group: com.example
  • Artifact: firstspring
  • Packaging: Jar
  • Java: 17 or a newer version installed on your machine
  • Dependency: Spring Web

Select Generate, unzip the download, and open the firstspring directory. The generated Maven wrapper (mvnw) means you do not need a separate Maven installation.

The important files are:

firstspring/
├── pom.xml
├── mvnw
└── src/main/java/com/example/firstspring/
    └── FirstspringApplication.java

@SpringBootApplication on the generated class combines configuration, auto-configuration, and component scanning. Keep new controllers and services in that package or a child package so the default scan finds them.

3. Add a response type

Create Greeting.java beside the generated application class:

package com.example.firstspring;

public record Greeting(String message) {}

A Java record is a compact fit for this immutable response. If records are new to you, the key point is simply that new Greeting("Hello, Spring!") creates a value with a message component.

4. Add the controller

Create GreetingController.java in the same package:

package com.example.firstspring;

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 {

    @GetMapping("/greeting")
    public Greeting greeting(
            @RequestParam(defaultValue = "Spring") String name) {
        return new Greeting("Hello, " + name + "!");
    }
}

@GetMapping maps HTTP GET requests to the method. @RequestParam reads the optional name query parameter. @RestController tells Spring MVC to write the returned object to the response body; the Spring Web starter supplies Jackson, which serializes it as JSON.

5. Run and verify it

On macOS or Linux:

./mvnw spring-boot:run

On Windows:

mvnw.cmd spring-boot:run

Then call the endpoint in another terminal:

curl "http://localhost:8080/greeting?name=Asha"

Expected response:

{"message":"Hello, Asha!"}

Stop the server with Ctrl+C. You can also create an executable jar with ./mvnw clean package and run the jar from target/ with java -jar.

Common first-run problems

  • Port 8080 is already in use: stop the other process or add server.port=8081 to src/main/resources/application.properties.
  • The endpoint returns 404: confirm the controller is below com.example.firstspring, or move the application class to a common parent package.
  • The wrapper is not executable: run chmod +x mvnw on macOS/Linux.
  • Compilation uses the wrong Java: compare java -version with ./mvnw -version; both should point to a supported JDK.

This tutorial deliberately stops at setup. Read how IoC and constructor injection work before adding services, or build the first layered MVC/REST application. If Java classes themselves are still unfamiliar, start with classes and objects in Java.

Primary references

These backend fundamentals also support the applied work described in the Forward-Deployed Engineer program.