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:

pom.xml

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>com.vaadin</groupId>
  5. <artifactId>vaadin-bom</artifactId>
  6. <!-- Declare the latest Vaadin version
  7. as a property or directly here -->
  8. <version>${vaadin.version}</version> <!-- <1> -->
  9. <type>pom</type>
  10. <scope>import</scope>
  11. </dependency>
  12. </dependencies>
  13. </dependencyManagement>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.vaadin</groupId>
  17. <artifactId>
  18. vaadin-spring-boot-starter
  19. </artifactId>
  20. <version>${vaadin.version}</version>
  21. </dependency>
  22. </dependencies>
  23. <build>
  24. <plugins>
  25. <!-- The Spring Boot Maven plugin for easy
  26. execution from CLI and packaging -->
  27. <plugin>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>
  30. spring-boot-maven-plugin
  31. </artifactId>
  32. </plugin>
  33. <!--
  34. Takes care of synchronizing java
  35. dependencies and imports in package.json and
  36. main.js files. It also creates
  37. webpack.config.js if does not exist yet.
  38. -->
  39. <plugin>
  40. <groupId>com.vaadin</groupId>
  41. <artifactId>vaadin-maven-plugin</artifactId>
  42. <version>${vaadin.version}</version>
  43. <executions>
  44. <execution>
  45. <goals>
  46. <goal>prepare-frontend</goal>
  47. <goal>build-frontend</goal>
  48. </goals>
  49. </execution>
  50. </executions>
  51. </plugin>
  52. </plugins>
  53. </build>
  1. Notice that the vaadin-bom dependency in the dependencyManagement 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:

Java

  1. @SpringBootApplication 1
  2. public class Application {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Application.class, args);
  5. }
  6. }
  1. 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:

Java

  1. @Route
  2. public class MainView extends VerticalLayout {
  3. public MainView() {
  4. add(new Text("Welcome to MainView."));
  5. }
  6. }

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.