Spring Boot 2.0.0+Vaadin 8.3打包WAR部署Tomcat后NPE求助
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
@ServletComponentScanto 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.xmlincludes themaven-war-pluginconfiguration to package theVAADINdirectory 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 packageto rebuild the WAR from scratch—sometimes old cached resources cause issues. - Clear Tomcat’s
workdirectory 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
@SpringUIand@SpringComponentto 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
@Autowiredis 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.xmland ensure Vaadin dependencies (likevaadin-spring-boot-starter) have a scope ofcompile(notprovided), unless you’ve explicitly added them to Tomcat’s classpath. - Remove any Vaadin-related JARs from Tomcat’s
libdirectory 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 thatserviceisnullon line 25, pointing you straight to an injection issue.
内容的提问来源于stack exchange,提问作者krishna

