Use Flow with Spring

The vaadin-spring add-on allows you to use Flow with Spring in two ways: as a Spring Boot application and as a Spring Web application.

Spring Boot

The easiest way to use Spring is the Spring Boot application. To use the add-on, you only need to write one very simple class.

Note
Please refer to the Spring Boot project and its documentation page for information about Spring Boot.
Note
There is another section Use Flow with Spring MVC in the tutorial which describes how to write pure Spring Web application without Spring Boot.

Java

  1. @SpringBootApplication
  2. public class ExampleServletInitializer extends SpringBootServletInitializer {
  3. }
Note
This code looks like magic and you may wonder why it works. The @SpringBootApplication annotation makes all the job for you under the hood.

Thanks to Spring Boot auto-configuration you only need the vaadin-spring add-on in your classpath and no Flow classes are needed in the class declaration. For vaadin-platform user, what you need to do is adding the vaadin-bom dependency to the dependencyManagement section in your pom.xml:

XML

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>com.vaadin</groupId>
  5. <artifactId>vaadin-bom</artifactId>
  6. <version>${vaadin.version}</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  12. <dependencies>
  13. <dependency>
  14. <groupId>com.vaadin</groupId>
  15. <artifactId>vaadin-spring-boot-starter</artifactId>
  16. <version>${vaadin.version}</version>
  17. </dependency>
  18. <!-- Spring -->
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. <version>${spring-boot.version}</version>
  23. <exclusions>
  24. <exclusion>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-tomcat</artifactId>
  27. </exclusion>
  28. </exclusions>
  29. </dependency>
Note
You can see the exclusion in the spring-boot-starter-web declaration. It’s not relevant for this example but if you want to run your application as a deployable WAR then this helps you avoid classpath collisions.
Note
The vaadin-spring add-on doesn’t declare compile dependency to Spring Boot (it doesn’t appear transitively) so you should declare it by yourself. This is done to be able to use the add-on without Spring Boot.

Of course you need at least one Flow related class in your application to be able to handle URLs. The minimal code would be:

Java

  1. @Route("")
  2. public class HelloComponent extends Div {
  3. public HelloComponent(){
  4. setText("Hello world!");
  5. }
  6. }

With the ExampleServletInitializer you can deploy the application as a WAR to a web server. Spring Boot lets you speed up the development process and run the Spring Boot application as a plain Java Application. For this you need to add a main method with the following content:

Java

  1. @SpringBootApplication
  2. public class ExampleServletInitializer extends SpringBootServletInitializer {
  3. public static void main(String[] args) {
  4. SpringApplication.run(ExampleServletInitializer.class, args);
  5. }
  6. }

No need to do anything in addition to that. Once you start this Java application, Spring Boot will start its embedded web server and everything will work in the same way.

Note
You should remove exclusion from the spring-boot-starter-web dependency in the pom.xml snippet above.

Note

Due to a bug when using vaadin-upload-flow component and use root mapping, then you need to disable the spring boot multipart upload by adding to application.properties the property spring.servlet.multipart.enabled = false.

This is already set in the spring starter, to follow the state of the problem see vaadin/spring#381