Documentation

Documentation versions (currently viewingVaadin 23)

You are viewing documentation for Vaadin 23. View latest documentation

Using Vaadin with Spring Boot

Spring Boot is a fast and easy way to create Vaadin applications, which work well with Spring. Spring Boot includes an embedded web server, so you don’t need to install one or handle deployment. Instead of deploying a Vaadin application to a servlet container, you run it as a Java application.

Vaadin applications created with the https://start.vaadin.com are by default Spring Boot applications.

The Vaadin Spring add-on allows you to use Vaadin with Spring Boot.

Note
Spring MVC Tutorial
See Using Vaadin with Spring MVC to learn how to use Vaadin in a more traditional Spring MVC web application, without Spring Boot.

The easiest way to create an application with Spring Boot and Vaadin is to start with a template application created by https://start.vaadin.com/. However, you can also add the required dependencies to your project manually.

Adding Dependencies

Like many other tech stacks on Spring Boot, Vaadin provides a starter dependency that includes all the essential modules and auto-configuration. Only the vaadin-spring-boot-starter dependency is required, but you should also declare the vaadin-bom if you need additional Vaadin dependencies. For production builds, you should also add the vaadin-maven-plugin, which generates the optimized JavaScript packages.

Example Vaadin Spring Boot dependencies in pom.xml.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-bom</artifactId>
            <!-- declare the latest Vaadin version
                 as a property or directly here -->
            <version>${vaadin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>
            vaadin-spring-boot-starter
        </artifactId>
        <version>${vaadin.version}</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <!-- The Spring Boot Maven plugin for easy
             execution from CLI and packaging -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>
                spring-boot-maven-plugin
            </artifactId>
        </plugin>

        <!--
            Takes care of synchronizing java
            dependencies and imports in package.json and
            main.js files. It also creates
            vite.config.ts if doesn't exist yet.
        -->
        <plugin>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-maven-plugin</artifactId>
            <version>${vaadin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-frontend</goal>
                        <goal>build-frontend</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • The vaadin-bom dependency in the dependencyManagement section declares the versions of modules in the current Vaadin release.

Running Spring Boot Applications

Spring Boot applications are executed via the traditional main() method. If a Vaadin Spring dependency is in your classpath, Spring Boot automatically starts a web server and configures Vaadin with Spring. If you created your project via https://start.vaadin.com or https://start.spring.io, an application class with the main() method is already available for you.

Example: Application class.

@SpringBootApplication
public class Application {

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

}
  • The @SpringBootApplication annotation enables Spring Boot under the hood. This includes the Spring configuration, component scanning and auto-configuration.

Tip
Follow the instructions in the Spring Boot documentation if you want to deploy your Spring Boot application as a traditional WAR file.

Adding Vaadin View to a Spring Boot Application

With Vaadin, views are defined as Java classes using the @Route annotation. At application start-up, the classes are detected and published in a path derived from the class name or defined as a parameter to the annotation.

Example: MainView class.

@Route
public class MainView extends VerticalLayout {

	public MainView() {
		add(new Text("Welcome to MainView."));
	}

}
Tip
If you don’t provide a path parameter, the framework derives the path from the class name. The derived name is in lower case, and any trailing "View" is removed. Also, the names MainView or Main are mapped to root (that is, the path is "").

Vaadin Spring Boot Examples

Vaadin Spring Examples include example applications that showcase the basic usage of Vaadin and Spring Boot. You can use them to test the concepts and features covered in this documentation.

744C143E-D8BF-4A9D-8CBA-28B382A32598