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
@SpringBootApplication
public class ExampleServletInitializer extends SpringBootServletInitializer {
}
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
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<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>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</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
@Route("")
public class HelloComponent extends Div {
public HelloComponent(){
setText("Hello world!");
}
}
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
@SpringBootApplication
public class ExampleServletInitializer extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ExampleServletInitializer.class, args);
}
}
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