Jmork
Jmork

Reputation: 33

How to add Vaadin to an existing Spring Boot project and make Vaadin actually start/build my frontend plus routes?

Hellо аll,

I have a Spring-boot project that currently doesn't have any frontend/UI.

Here is the public repository that you can find all of the code. Notice that it won't run without an SQL server set up in application.properties file/environment variables.

Here is another repo of a project that actually uses/starts Vaadin and builds the frontend.

I tried adding Vaadin dependencies into my POM.xml.

Here are the dependencies I added:

 <properties>
        <java.version>11</java.version>
        <vaadin.version>24.0.5</vaadin.version>
    </properties>
    <!--adding vaadin!-->
    <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>
    <!--adding vaadin!-->
    <dependencies>

        <!-- adding vaadin!-->
        <dependency>
            <groupId>com.vaadin</groupId>
            <!-- Replace artifactId with vaadin-core to use only free components -->
            <artifactId>vaadin</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
            <version>${vaadin.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.vaadin/vaadin-maven-plugin -->
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-maven-plugin</artifactId>
            <version>24.0.5</version>
        </dependency>

        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring</artifactId>
            <version>3.2.1</version>
        </dependency>
            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>${vaadin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-frontend</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>

I also added a simple class LoginView in order to try whether the route and ui works:

@Route(value = "login")
@PageTitle("Login | University Project")
@AnonymousAllowed
public class LoginView extends VerticalLayout implements BeforeEnterObserver {

    private final LoginForm login = new LoginForm();

    public LoginView() {

        addClassName("login-view");
        setSizeFull();
        setAlignItems(Alignment.CENTER);
        setJustifyContentMode(JustifyContentMode.CENTER);
        getStyle().set("background", "869fcb");
        Button login1 = new Button("Login");
        login.setAction("login");

        //////////////
        //HorizontalLayout back = new HorizontalLayout();
        //back.setSizeFull();
        //back.getStyle().set("background","red");
        //////////////
        add(toggleButtonTheme, new H1("University Project"), login, registrationButton);
    }

    Button toggleButtonTheme = new Button("Toggle dark theme", click -> {
        ThemeList themeList = UI.getCurrent().getElement().getThemeList(); // (1)

        if (themeList.contains(Lumo.DARK)) { // (2)
            themeList.remove(Lumo.DARK);
        } else {
            themeList.add(Lumo.DARK);
        }
    });

    Button registrationButton = new Button("Register", event -> UI.getCurrent().navigate("registration"));

    @Override
    public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
        // inform the user about an authentication error
        if (beforeEnterEvent.getLocation()
                .getQueryParameters()
                .getParameters()
                .containsKey("error")) {
            login.setError(true);
        }

    }
}

Then "localhost:8888/login" page returns the "Whitelabel Error Page"...

I tried running "mvn clean install"/"mvn package"/"mvn deploy"/etc. - all the maven commands and then running the application from the jar itself - Vaadin doesn't start as in this tutorial.

I also tried to add "vaadin:build-frontend" and every other vaadin plugin command before my Run configuration, yet it doesn't start Vaadin.

Expected outcome/run log:

##excluded some log events here##

2023-05-18 10:34:24.155  INFO 20288 --- [  restartedMain] c.v.f.s.DefaultDeploymentConfiguration   : 
Vaadin is running in DEVELOPMENT mode - do not use for production deployments.
2023-05-18 10:34:24.212  INFO 20288 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-05-18 10:34:24.225  INFO 20288 --- [  restartedMain] com.uniproject.application.Application   : Started Application in 8.524 seconds (JVM running for 9.355)
2023-05-18 10:34:24.230  INFO 20288 --- [onPool-worker-1] c.v.b.devserver.AbstractDevServerRunner  : Starting Vite
2023-05-18 10:34:24.853  INFO 20288 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-05-18 10:34:24.853  INFO 20288 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-05-18 10:34:24.855  INFO 20288 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms

------------------ Starting Frontend compilation. ------------------
2023-05-18 10:34:33.325  INFO 20288 --- [onPool-worker-1] c.v.b.devserver.AbstractDevServerRunner  : Running Vite to compile frontend resources. This may take a moment, please stand by...
2023-05-18 10:34:36.774  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   : 
2023-05-18 10:34:36.774  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   :   VITE v3.2.4  ready in 3377 ms

----------------- Frontend compiled successfully. -----------------

2023-05-18 10:34:36.774  INFO 20288 --- [onPool-worker-1] c.v.b.devserver.AbstractDevServerRunner  : Started Vite. Time: 12544ms
2023-05-18 10:34:36.774  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   : 
2023-05-18 10:34:36.775  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   :   ➜  Local:   http://127.0.0.1:61319/VAADIN/
2023-05-18 10:34:38.045  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   : 
2023-05-18 10:34:38.046  INFO 20288 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   : [TypeScript] Found 0 errors. Watching for file changes.

Actual outcome/run log:

##excluded some event logs here##

2023-05-18 10:35:50.078  INFO 18152 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-05-18 10:35:50.078  INFO 18152 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-05-18 10:35:51.601  INFO 18152 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-05-18 10:35:51.791  INFO 18152 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 174 ms. Found 8 JPA repository interfaces.
2023-05-18 10:35:52.973  INFO 18152 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
2023-05-18 10:35:52.985  INFO 18152 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-05-18 10:35:52.985  INFO 18152 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.44]
2023-05-18 10:35:53.139  INFO 18152 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-05-18 10:35:53.139  INFO 18152 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3060 ms
2023-05-18 10:35:53.389  INFO 18152 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-05-18 10:35:53.459  INFO 18152 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.29.Final
2023-05-18 10:35:53.639  INFO 18152 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2023-05-18 10:35:53.823  INFO 18152 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-05-18 10:35:54.118  INFO 18152 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-05-18 10:35:54.158  INFO 18152 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.SQLServer2012Dialect
2023-05-18 10:35:55.117  INFO 18152 --- [  restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-05-18 10:35:55.127  INFO 18152 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-05-18 10:35:56.214  WARN 18152 --- [  restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-05-18 10:35:56.476  INFO 18152 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2023-05-18 10:35:56.495  WARN 18152 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : Unable to start LiveReload server
2023-05-18 10:35:56.795  INFO 18152 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
2023-05-18 10:35:57.445  INFO 18152 --- [  restartedMain] com.finals.cinema.CinemaApplication      : Started CinemaApplication in 8.049 seconds (JVM running for 9.291)

Thanks for the help in advance.

Upvotes: 0

Views: 482

Answers (1)

Marco C
Marco C

Reputation: 981

Vaadin 24 requires Spring Boot 3.x, but it seems your project is using 2.4. You should either upgrade Spring Boot, or use Vaadin 23.3

Upvotes: 1

Related Questions