Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Running and the embedded server

A Spring Boot application needs no separately installed web server. The Tomcat server is already inside as an ordinary library and starts together with the program. So running it takes one command.

The first address

For the application to answer something, let's add a tiny controller. Create a file next to BlogApplication.java:

// src/main/java/com/example/blog/HelloController.java
package com.example.blog;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
 
    @GetMapping("/hello")
    public String hello() {
        return "Hello from Spring Boot!";
    }
}

@RestController tells Spring that the class handles HTTP requests, and @GetMapping("/hello") binds the method to the /hello address. Controllers are covered in detail in chapter three.

Running

From the project folder run:

./mvnw spring-boot:run

On Windows (cmd or PowerShell) the command is mvnw.cmd spring-boot:run. The first run is slow: Maven Wrapper downloads Maven and the libraries. At the end of the log you will see (lines shortened):

 :: Spring Boot ::                (v4.1.1)
 
INFO ... Starting BlogApplication using Java 25.0.4.1 with PID 12236
INFO ... No active profile set, falling back to 1 default profile: "default"
INFO ... Tomcat initialized with port 8080 (http)
INFO ... Starting Servlet engine: [Apache Tomcat/11.0.24]
INFO ... Tomcat started on port 8080 (http) with context path '/'
INFO ... Started BlogApplication in 3.008 seconds

The Tomcat started on port 8080 line means the server is waiting for requests. The program does not exit on its own — it runs until you press Ctrl+C.

Checking it

Open http://localhost:8080/hello in a browser or call the address from another terminal:

curl -i http://localhost:8080/hello
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
 
Hello from Spring Boot!

The -i flag shows the status line and the response headers. If you request an address that does not exist, Spring Boot returns a 404 response in JSON by itself.

Another port

The port is set in the settings file:

# src/main/resources/application.properties
spring.application.name=blog
server.port=8081

After a restart the application opens at http://localhost:8081. You can also pass the port at startup without editing the file: ./mvnw spring-boot:run -Dspring-boot.run.arguments=--server.port=8081.

Restarting after changes

Every change to Java code needs a restart: Ctrl+C and ./mvnw spring-boot:run again. In IntelliJ IDEA it is handier to run the BlogApplication class with the green button next to the main method. The Spring Boot DevTools module from start.spring.io can restart the application itself when the code is rebuilt.

Common mistake

If you start the application a second time without stopping the first one, the startup breaks off:

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Web server failed to start. Port 8080 was already in use.

Port 8080 is already taken — by the first copy of the program or by another server. Stop the earlier run with Ctrl+C in its terminal or change server.port.

Thanks for reading!

Donate to the site — we work hard for you.