Your First Application

The entry point

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

@SpringBootApplication is three annotations in one:

AnnotationJob
@SpringBootConfigurationthis class may declare beans
@ComponentScanfind components in this package and below
@EnableAutoConfigurationconfigure what the classpath suggests
Warning

Component scanning starts at the application class’s package. Keep it at the root — classes in a sibling package are invisible.

A first endpoint

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(@RequestParam(defaultValue = "world") String name) {
        return "Hello, " + name + "!";
    }
}
curl "localhost:8080/hello?name=Ann"     # Hello, Ann!

No web.xml, no servlet registration: spring-boot-starter-web puts Tomcat on the classpath, and auto-configuration starts it.

Returning JSON

Return an object and Jackson serializes it.

public record Greeting(String message, Instant at) {}

@GetMapping("/greeting")
public Greeting greeting() {
    return new Greeting("Hello", Instant.now());
}
{"message":"Hello","at":"2026-03-01T10:15:30.00Z"}

Configuration

src/main/resources/application.properties:

server.port=8081
spring.application.name=demo
logging.level.com.example=DEBUG

Or YAML, in application.yaml:

server:
  port: 8081
logging:
  level:
    com.example: DEBUG

What happens on startup

  1. SpringApplication.run creates an application context
  2. component scan collects your @Component/@Service/@RestController classes
  3. auto-configuration adds what the classpath implies (web server, DataSource, …)
  4. beans are created and wired
  5. the embedded Tomcat starts and binds a port
./mvnw spring-boot:run -Ddebug           # prints the auto-configuration report

★ Exercises

  1. Add a /time endpoint returning the current time as JSON.
  2. Change the port to 9000 in application.properties.
  3. Move HelloController into com.example.other — what happens, and why?
  4. Run with -Ddebug and find three auto-configurations that matched.