You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.0.0+Vaadin 8.3打包WAR部署Tomcat后NPE求助

Troubleshooting Vaadin NullPointerException on Tomcat Deployment (Spring Boot 2.0.0 + Vaadin 8.3)

Hey there, sorry to hear you're stuck with this NPE when moving your Spring Boot + Vaadin app from Eclipse to Tomcat! I’ve dealt with similar deployment quirks before, so let’s walk through the most likely fixes.

Common Causes & Solutions

1. Vaadin Servlet Registration Issues

Spring Boot’s auto-configuration works great for embedded servers, but sometimes fails to properly register the VaadinServlet when deploying as a WAR to external Tomcat. This can lead to uninitialized Vaadin services that throw NPEs.

Fixes:

  • Add @ServletComponentScan to your Spring Boot main application class to ensure Vaadin’s servlets are detected.
  • Or manually register the VaadinServlet via a ServletRegistrationBean:
    @Configuration
    public class VaadinServletConfig {
        @Bean
        public ServletRegistrationBean<VaadinServlet> vaadinServlet() {
            VaadinServlet servlet = new VaadinServlet();
            ServletRegistrationBean<VaadinServlet> registration = new ServletRegistrationBean<>(servlet, "/*");
            registration.setLoadOnStartup(1); // Force early initialization
            return registration;
        }
    }
    

2. Missing Vaadin Static Resources

Vaadin relies on static resources (themes, widgetsets, client-side code) stored in the VAADIN directory. If these aren’t included in your WAR, the UI can’t initialize properly and throws NPEs.

Fixes:

  • Verify your pom.xml includes the maven-war-plugin configuration to package the VAADIN directory correctly:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
            <webResources>
                <resource>
                    <directory>${project.build.directory}/VAADIN</directory>
                    <targetPath>VAADIN</targetPath>
                </resource>
            </webResources>
        </configuration>
    </plugin>
    
  • Run mvn clean package to rebuild the WAR from scratch—sometimes old cached resources cause issues.
  • Clear Tomcat’s work directory to remove any stale compiled classes or resources.

3. Spring Dependency Injection Timing

If your Vaadin UI tries to access Spring-managed beans before they’re fully initialized, you’ll get an NPE. This often happens because Vaadin’s UI initialization runs before Spring’s context is fully ready in external Tomcat.

Fixes:

  • Ensure your UI class is annotated with @SpringUI and @SpringComponent to let Spring manage it:
    @SpringUI
    @SpringComponent
    public class MainUI extends UI {
        @Autowired
        private MyBusinessService service; // This bean will be injected properly
    
        @Override
        protected void init(VaadinRequest request) {
            // Double-check dependencies before using them (for debugging)
            if (service == null) {
                throw new IllegalStateException("Business service not injected!");
            }
            // Proceed with UI setup
        }
    }
    
  • Avoid using constructor injection in Vaadin UIs—field injection with @Autowired is more reliable here because Vaadin creates UI instances via its own provider.

4. Tomcat Classloader Conflicts

External Tomcat uses a different classloading hierarchy than Spring Boot’s embedded server. If Vaadin dependencies are marked as provided in your pom.xml but Tomcat doesn’t include them, or if there are conflicting versions in Tomcat’s lib directory, you’ll get unexpected NPEs.

Fixes:

  • Check your pom.xml and ensure Vaadin dependencies (like vaadin-spring-boot-starter) have a scope of compile (not provided), unless you’ve explicitly added them to Tomcat’s classpath.
  • Remove any Vaadin-related JARs from Tomcat’s lib directory to avoid version conflicts.

Critical Next Step: Dig Into the Full Stack Trace

The exception you shared is a wrapper (ServiceException around NullPointerException). To pinpoint the exact cause, look for the root cause in the full stack trace—it will tell you which line of code is trying to access a null object. For example:

Caused by: java.lang.NullPointerException
at com.yourpackage.MainUI.init(MainUI.java:25)
This would immediately tell you that service is null on line 25, pointing you straight to an injection issue.

内容的提问来源于stack exchange,提问作者krishna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:43:18