Spring Boot first web application

last modified July 29, 2023

Spring Boot first web application tutorial shows how to create a simple Spring Boot web application. The current trend is to launch Spring Boot applications from an executable JAR. (See SpringBootServletInitializer tutorial for an example of a traditional WAR deployment.)

Spring is a popular Java application framework. Spring Boot is an effort to create stand-alone, production-grade Spring based applications with minimal effort.

Spring Boot web application example

The application shows a message and today's date. The message is retrieved from an appplication's property.

│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
│       │   application.properties
│       └───templates
│               index.peb

This is the project structure.

plugins {
    id 'org.springframework.boot' version '3.1.1'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'

group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {

dependencies {
    implementation 'io.pebbletemplates:pebble-spring-boot-starter:3.2.1'
    implementation 'org.springframework.boot:spring-boot-starter-web'

This is the Gradle build file. The spring-boot-starter-web is starter for building web, including RESTful, applications using Spring MVC.

The pebble-spring-boot-starter contains the Pebble template engine. When Spring Boot detects this starter, it automatically configures Pebble for us.

The application is packaged into a JAR file, which contains an embedded Tomcat web server.

application.message=Hello there

The application.properties file contains various configuration settings of a Spring Boot application. We have one custom message option.

package com.zetcode.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.time.LocalDate;
import java.util.Map;

public class MyController {

    private String message = "Hi there";

    public String index(Model model) {

        model.addAttribute("now", LocalDate.now());
        model.addAttribute("message", this.message);

        return "index";

This is the controller class for the Spring Boot web application. A controller is decorated with the @Controller annotation. The controller has one mapping. The mapping resolves to the index.peb, which is located in the resources/templates directory.

private String message = "Hi there";

We inject a value from the application.properties into the message variable.

public String index(Model model) {

    model.addAttribute("now", LocalDate.now());
    model.addAttribute("message", this.message);

    return "index";

The @GetMapping annotation maps a GET request with the / path to the index method handler. A model is created and filled with data. Spring Boot resolves the index view to the index.peb template file, to which it also sends the model data.

<!DOCTYPE html>
<html lang="en">
    <title>Home page</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

Today: {{ now }}

Message: {{ message }}


The index.peb displays two values: the current date and the received message. Both values are passed to the template via the controller.

Today: {{ now }}

Pebble uses the {{}} syntax to display the variable.

package com.zetcode;

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

public class Application {

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

The Application sets up the Spring Boot application.

$ ./gradlew bootRun

We run the application. Now we can navigate to localhost:8080 to see the application message.

In this article we have created our first Spring Boot web application.


My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Spring Boot tutorials.