Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 66 ยท Getting Started

Your First Application

Write and run your first Spring Boot app: @SpringBootApplication, a hello endpoint, starting and stopping, reading the startup log and the banner.

Time to write something that answers an HTTP request. A first Spring Boot application is about fifteen lines of Java, and yet starting it triggers an impressive amount of work: scanning, configuration, a web server, a log full of messages. Learning to read that log is one of the most valuable early skills, because it tells you exactly what Boot decided for you.

In this lesson you will look at the one annotation every Boot app starts with, add a hello endpoint, start and stop the application, read the startup log line by line and customize the banner. The real application cannot run in our sandbox, so it is shown read-only; two runnable stand-ins model the endpoint dispatch and the log reading.

The application class

You met this class in What Is Spring Boot?; here it is again, this time with the annotation opened up (read-only):

Java
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 a convenience annotation. It is shorthand for three others:

One annotation, three jobs

@SpringBootConfiguration This class can declare beans, like @Configuration.
@EnableAutoConfiguration Create infrastructure beans from the classpath.
@ComponentScan Find annotated classes in this package and below.

You can write the three annotations yourself if you need finer control.

1 / 2

The args parameter matters: command-line arguments like --server.port=9000 are passed to Boot and become configuration. The method returns the application context, which you rarely need.

A hello endpoint

With the web starter on the classpath, add a controller next to the application class (read-only):

Java
package com.example.demo;

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

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(@RequestParam(defaultValue = "World") String name) {
        return "Hello, " + name + "!";
    }
}

@RestController tells Spring MVC that this class handles web requests and that return values are written straight into the response body. @GetMapping("/hello") maps GET /hello to the method. @RequestParam binds the name query parameter and falls back to "World".

Because the class sits in the root package, scanning finds it, and Spring MVC registers the mapping. You never register the controller yourself.

Running and stopping

Start the application from your IDE (run the main method) or from the terminal:

TEXT
./mvnw spring-boot:run          # Maven
./gradlew bootRun               # Gradle
java -jar target/demo-0.0.1-SNAPSHOT.jar    # after packaging

Then call it:

TEXT
$ curl "http://localhost:8080/hello"
Hello, World!

$ curl "http://localhost:8080/hello?name=Ada"
Hello, Ada!

Stop the application with Ctrl+C. Boot registers a shutdown hook, so it closes the context, stops the web server and releases resources before the JVM exits. Later you will configure graceful shutdown so in-flight requests finish first.

The most common first error is Port 8080 was already in use: another program (often a previous run you forgot to stop) is listening. Stop that process, or choose another port in application.properties:

PROPERTIES
server.port=9090

Our first TryIt models how an annotated method becomes an endpoint: a stand-in dispatcher scans a class for methods annotated with our own @MiniGet, builds a routing table and answers "requests".

Java From annotation to endpoint

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

What is real: annotations on methods are metadata, and the framework reads them with reflection to build a routing table at startup. What is the stand-in: Spring MVC adds argument binding, conversion, content negotiation, interceptors and much more, and listens on a real socket.

Reading the startup log

Every start prints a log like this (shortened, real output format of a Boot application; the exact lines vary by version):

TEXT
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v4.0.0)

2026-03-02T10:15:30.123Z  INFO 18422 --- [demo] [  main] c.example.demo.DemoApplication : Starting DemoApplication using Java 21 with PID 18422
2026-03-02T10:15:30.125Z  INFO 18422 --- [demo] [  main] c.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2026-03-02T10:15:31.002Z  INFO 18422 --- [demo] [  main] o.s.boot.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2026-03-02T10:15:31.640Z  INFO 18422 --- [demo] [  main] o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2026-03-02T10:15:31.651Z  INFO 18422 --- [demo] [  main] c.example.demo.DemoApplication : Started DemoApplication in 1.9 seconds (process running for 2.3)

Read it from the top:

  • The banner and the Boot version tell you which release is running.
  • Starting ... using Java 21 with PID ... shows the JVM and process id, useful when several apps run on one machine.
  • No active profile set says which configuration profile applies. You will use profiles in Profiles and Environment-Specific Config.
  • Tomcat initialized/started on port 8080 confirms the embedded server and port. A missing line means no web server was configured.
  • Started DemoApplication in 1.9 seconds is the last line to appear when everything worked. If you do not see it, scroll up for the first ERROR.

Each line has a fixed shape: timestamp, level, process id, application name, thread, logger name, message. The second TryIt parses log lines like these, which is also how log tools extract fields.

Java Read a startup log

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Customizing the banner

The ASCII banner is optional decoration. Put a text file named banner.txt in src/main/resources and Boot prints it instead; placeholders such as ${spring-boot.version} are replaced. To switch it off:

PROPERTIES
spring.main.banner-mode=off

The banner is harmless, but turning it off keeps logs shorter in production, where every line is collected and paid for.

What to try next

Add a second method such as @GetMapping("/time") returning the current time, restart, and watch the startup log: nothing changes except the work the mapping does. Then rename the package of HelloController to com.example.other and restart. The server starts but /hello answers 404, because the class is outside the scanned packages. Seeing that failure once explains many future questions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which three annotations does @SpringBootApplication combine?

    It marks the class as a configuration source, turns on auto-configuration and scans the package and sub-packages for components.
  2. 2. Your app fails with "Port 8080 was already in use". What is a valid fix?

    Two programs cannot listen on the same port. Free the port or choose another one with server.port.
  3. 3. Which log line shows that the application started successfully?

    The "Started ..." line is logged last, after the context is refreshed and the server is running.
  4. 4. A controller in package com.example.other is never called, although the main class is in com.example.demo. Why?

    Scanning starts at the package of the main class. A sibling package is not scanned.

Summary

  • @SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan.
  • SpringApplication.run reads configuration, builds the context, starts the embedded server and logs "Started ...".
  • @RestController with @GetMapping turns a method into an endpoint; the framework finds it by scanning.
  • Start with ./mvnw spring-boot:run, ./gradlew bootRun or java -jar; stop with Ctrl+C; fix "port in use" by freeing or changing the port.
  • The startup log tells you the Boot version, Java version, profiles, port and start time; when something fails, look for the first ERROR.
  • A banner.txt customizes the banner and spring.main.banner-mode=off hides it.

Next: Starters and Dependency Management.