Using Vaadin With Spring Boot
The Vaadin Spring add-on allows you to use Vaadin with Spring Boot.
Spring Boot speeds up the development process and provides a fast and efficient development environment by emphasizing usability. It is the easiest way to use the Spring framework.
Vaadin Start is the easiest way to create an application with Spring Boot and Vaadin. You can also configure and download a Vaadin project using Spring Initializr or add the required dependencies manually to your project.
If you prefer not to use Spring Boot, see Using Vaadin with Spring MVC to learn how to use Vaadin in a more traditional Spring MVC web application.
Adding Dependencies
Like many other tech stacks on Spring Boot, Vaadin provides a starter dependency that provides all essential modules and auto-configuration.
Only the vaadin-spring-boot-starter
dependency is needed, but it is suggested to also declare vaadin-bom
if you need additional Vaadin dependencies.
For production builds, it is also suggested to add vaadin-maven-plugin
, which generates the optimized JavaScript packages.
The following example shows 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> <!-- <1> -->
<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
webpack.config.js if does not 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>
-
Notice that the
vaadin-bom
dependency in thedependencyManagement
section declares the versions of modules in current Vaadin release.
Running Spring Boot Applications
Spring Boot applications are executed through the traditional main()
method.
If Vaadin Spring dependency is on your classpath, Spring Boot automatically starts a web server and configures Vaadin with Spring.
If you created your project at start.vaadin.com
or start.spring.io
, an application class with the main()
method is already available to you.
The following example shows a minimal implementation of the Application
class:
@SpringBootApplication // (1)
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 Spring configuration, component scanning and auto-configuration.
If you want to deploy your Spring Boot application as a traditional WAR file, follow the instructions in Spring Boot documentation.
Add Vaadin View to Spring Boot application
In Vaadin Flow, a view is defined as a composite component.
The Java class is annotated with the @Route
annotation.
The routes are detected at the application start and published in a path derived from the class name or defined as a parameter to the annotation.
The following example shows typical a main view of an application with the root route:
@Route
public class MainView extends VerticalLayout {
public MainView() {
add(new Text("Welcome to MainView."));
}
}
If you omit the path parameter from @Route
, the framework derives the path from the class name.
The derived name will be in lower case, and a possible trailing "View" will be removed.
Also, the class names MainView
and Main
are mapped to root path (the path will be ""
).
Examples of Using Vaadin Spring Boot
Vaadin Spring Examples is an example application that showcases basic usage of Vaadin and Spring Boot. You can use it to test the concepts and features covered in this documentation.
8BAAC835-14AA-471A-9924-251D7502FB73