Encountering an ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean can be a frustrating roadblock for any developer working with Spring Boot. This specific exception signals a fundamental misconfiguration or missing dependency that prevents Spring Boot from launching its embedded web server, which is crucial for any web application. Essentially, your application context, responsible for managing your Spring beans, cannot find the necessary component to create and manage the web server environment. This often boils down to a simple oversight in dependency management or an incorrect project setup. Understanding the root cause is the first step toward a swift resolution, ensuring your development workflow remains smooth and efficient. This article will delve into the intricacies of this error, explore its common culprits, and provide clear, actionable steps to get your Spring Boot application up and running.
Understanding the ServletWebServerFactory Bean Error
The ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean typically arises when Spring Boot cannot locate a component responsible for creating the embedded web server. In a Spring Boot web application, the ServletWebServerApplicationContext is the specialized application context designed to manage web-specific components, including the embedded server (like Tomcat, Jetty, or Undertow). For this context to function, it needs a ServletWebServerFactory bean, which acts as a blueprint or factory for creating and configuring the actual web server instance.
Spring Boot’s powerful auto-configuration mechanism usually handles this seamlessly. When you include a web starter dependency like spring-boot-starter-web in your project, Spring Boot automatically detects its presence and configures the necessary ServletWebServerFactory bean. This bean then tells the application context how to create and start the embedded server. If this auto-configuration fails, or if the required dependency is absent, the application context will be unable to proceed with starting the web server, leading directly to the aforementioned exception.
Diagnosing this issue involves looking at how your project is configured and what dependencies are declared. The absence of this factory bean is a strong indicator that the application doesn’t “know” it’s supposed to be a web application, or it’s missing the critical pieces to become one. As a developer, recognizing this error immediately points you towards your project’s build file (pom.xml for Maven or build.gradle for Gradle) as the primary place to investigate.
Common Causes and Diagnoses
Identifying the exact cause of the ApplicationContextException related to the missing ServletWebServerFactory bean is key to resolving it efficiently. Most often, this error points to issues within your project’s build configuration or an incorrect application type. The Spring Boot ecosystem relies heavily on “starter” dependencies for simplified setup, and their omission is a frequent culprit.
One of the most prevalent reasons is the absence of the spring-boot-starter-web dependency. This starter bundles all the essential components for building web, including RESTful, applications using Spring MVC and an embedded Tomcat server. Without it, Spring Boot doesn’t have the necessary libraries to provide the ServletWebServerFactory bean. According to the Spring Boot documentation, “The spring-boot-starter-web provides all the necessary dependencies to build a web application, including an embedded web server.” If you’re building a web application, this starter is almost always mandatory.
Another common cause is inadvertently setting your Spring Boot application type to something other than a web application. For example, if you explicitly configure your application as a “none” or “reactive” type in your application.properties or application.yml file (e.g., spring.main.web-application-type=none), Spring Boot will correctly assume it doesn’t need to start a traditional servlet web server, thus not providing the ServletWebServerFactory bean. Similarly, if you’re attempting to run a Spring Boot batch application or a simple command-line application that doesn’t require a web server, but you’ve mistakenly included web-related code, you might encounter this error if other web dependencies are present but the context is not correctly initialized for web. Debugging these scenarios requires careful inspection of your configuration files and the main application class.
Visual representation showing: 1. Check pom.xml/build.gradle for spring-boot-starter-web. 2. Verify main application class. 3. Review application properties for web-application-type.
Resolving the ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean often involves a few straightforward checks and modifications to your project. Hereβs a methodical approach to troubleshoot and fix this issue:
-
Verify
spring-boot-starter-webDependencyThe most common fix is ensuring your project includes the
spring-boot-starter-webdependency. This starter is crucial for any traditional servlet-based Spring Boot web application. If you are using Maven, add the following to yourpom.xml:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>For Gradle users, include this in your
build.gradlefile:implementation 'org.springframework.boot:spring-boot-starter-web'After adding, remember to refresh your project’s dependencies (e.g., Maven Reimport in IntelliJ IDEA, or
gradle clean buildfrom the command line). -
Check Your Main Application Class
Ensure your main application class is correctly annotated with
@SpringBootApplication. This annotation implicitly enables auto-configuration, component scanning, and defines configuration properties. If you’ve manually excluded web auto-configuration or have a custom setup, verify that@EnableAutoConfigurationis correctly applied and not filtering out web-related configurations. For instance, sometimes developers mistakenly use@SpringBootApplication(exclude = {WebMvcAutoConfiguration.class})which would prevent the necessary web components from being configured. -
Inspect
application.propertiesorapplication.ymlConfirm that you haven’t explicitly disabled the web application type. Look for properties like
spring.main.web-application-type=noneorspring.main.web-application-type=reactivein your configuration files. If your application is indeed a servlet-based web application, these properties should either be absent or set toservlet. For a comprehensive guide on different application types, Baeldung offers an excellent overview of Spring Boot web application types.
Preventive Measures and Best Practices
Preventing the ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean error in the future involves adopting robust development practices, particularly around dependency management and project setup. Proactive steps can save significant debugging time and ensure a smoother development cycle. Here are some key best practices:
-
Utilize Spring Initializr for New Projects: When starting a new Spring Boot project, always use the Spring Initializr. This tool helps you generate a project with the Question & Answer :
I have written a spring batch application using Spring boot. When I am trying to run that application using command line and classpath on my local system it is running fine. However, when I tried to run it on linux server it is giving me following exceptionUnable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.Below is the way I am running it:
java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.logCase 1:
@SpringBootApplicationannotation missing in your spring boot starter class.Case 2:
For non-web applications, disable
web application typein the properties file.In
application.properties:spring.main.web-application-type=noneIf you use
application.ymlthen add:spring: main: web-application-type: noneFor web applications, extends
*SpringBootServletInitializer*in the main class.@SpringBootApplication public class YourAppliationName extends SpringBootServletInitializer{ public static void main(String[] args) { SpringApplication.run(YourAppliationName.class, args); } }Case 3:
If you use
spring-boot-starter-webfluxthen also addspring-boot-starter-webas dependency.Case 4:
if you use netty then use below properties:
spring.main.web-application-type=reactiveor yml:
spring: main: web-application-type: reactive