Project setup and your first controller

Generate a project, understand the layout, and get an HTTP endpoint answering within a few minutes.

Generating the project

Spring Initializr produces the skeleton with a build file, a wrapper and a test already wired up. Choose the dependencies you need now — Web, Validation, Spring Data JPA, H2 — and add the rest later.

# download from start.spring.io, or use your IDE wizard
unzip demo.zip -d demo
cd demo

./mvnw spring-boot:run       # starts the app on http://localhost:8080
./mvnw test                  # runs the generated context test
./mvnw package               # builds target/demo-0.0.1-SNAPSHOT.jar
java -jar target/demo-0.0.1-SNAPSHOT.jar
PathWhat it holds
src/main/java/.../DemoApplication.javaThe @SpringBootApplication entry point
src/main/resources/application.propertiesConfiguration, or the equivalent application.yml
src/main/resources/static/Static assets served directly from the root path
src/test/java/Test sources, including the generated context-loads test
pom.xml or build.gradleDependencies, plugins and the Spring Boot parent or plugin
target/ or build/Compiled output; never commit it
💡
Spring Boot ships an embedded Tomcat, so the runnable jar is the deployment unit. There is no application server to install, and java -jar is the whole deployment story for a simple service.

The application class and a first endpoint

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication   // configuration + auto-configuration + component scan
public class DemoApplication {

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

@RestController
class GreetingController {

    @GetMapping("/hello")
    String hello() {
        return "Hello, Spring";
    }
}
  • @SpringBootApplication combines @Configuration, @EnableAutoConfiguration and @ComponentScan, and scans the package it sits in plus every sub-package.
  • Auto-configuration inspects the classpath: add a JDBC driver and a datasource appears, add spring-boot-starter-web and an embedded servlet container appears.
  • The container creates and wires your beans at startup. You declare what you need; you rarely call a factory or a lookup.
  • DevTools restarts the application when the classpath changes and disables template caching, which shortens the edit-run loop considerably.

FAQ

Maven or Gradle?
Both are first-class with Spring Boot. Maven appears in most official examples and is easy to read; Gradle is more flexible and faster to build on large projects. Pick one per team.
Why is my port still 8080 after I changed it?
The property name matters: server.port=9090 in application.properties, or the SERVER_PORT environment variable. Also check that nothing else on the machine already holds the port.

REST controllers and dependency injection Configuration, JPA data access and profiles

Last refreshed 2026-09-18.