Search code examples
javaspring-bootvaadinvaadin7

Custom SpringVaadinServlet not detected


I'm trying to use a custom SpringVaadinServlet in my Vaadin app (that uses Spring Boot). Here's a sample app, in which the SpringVaadinServlet is not detected. I've read the Spring Vaadin tutorial but still can't get it to work. Here's the code (I have no XML conf):

@SpringBootApplication
public class VaadinSb2Application {

    public static void main(String[] args) {
        SpringApplication.run(VaadinSb2Application.class, args);
    }

    @Configuration
    @EnableVaadin
    public class AppConfig {}

    @SpringUI
    public class MyUI extends UI {

        private static final long serialVersionUID = 1L;

        @Override
        protected void init(VaadinRequest request) {
            setContent(new Label("Welcome!"));
        }
    }

    @WebServlet(urlPatterns = "/*", name = "MyServlet", asyncSupported = true)
    public static class MyServlet extends SpringVaadinServlet {

        private static final long serialVersionUID = 1L;

        public MyServlet() {
            System.out.println("servlet constr");
        }

        @Override
        protected void servletInitialized() throws ServletException {
            super.servletInitialized();
            System.out.println("servlet init");
        }
    }
}

Here are links to the Vaadin documentation: http://vaadin.github.io/spring-tutorial/ https://vaadin.com/docs/-/part/framework/advanced/advanced-spring.html

I use Spring Boot 1.4.2 and the latest Vaadin version.


Solution

  • When you use Spring Boot with Vaadin you should not register a servlet by using the @WebServlet annotation. The following should work to the override default Vaadin Spring Boot servlet:

    @Component("vaadinServlet")
    public class MySpringVaadinServlet extends SpringVaadinServlet {
    }