ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
Asked Answered
W

30

192

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 exception

Unable 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.log
Waft answered 8/5, 2018 at 10:45 Comment(1)
W
44

The solution is:

I explicitly set the below property to none in application.yml file.

spring:
  main:
    web-application-type: none
Waft answered 9/5, 2018 at 10:17 Comment(0)
P
290

Case 1:

@SpringBootApplication annotation missing in your spring boot starter class.

Case 2:

For non-web applications, disable web application type in the properties file.

In application.properties:

spring.main.web-application-type=none

If you use application.yml then add:

  spring:
    main:
      web-application-type: none

For 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-webflux then also add spring-boot-starter-web as dependency.

Case 4:

if you use netty then use below properties:

spring.main.web-application-type=reactive

or yml:

spring:
    main:
      web-application-type: reactive
Pelasgian answered 8/5, 2018 at 11:21 Comment(4)
Thanks for replying, however my application is not a web applicationWaft
@VijayantBhatia did you figure this out? I'm running a non web application in spring boot and getting the same error.Moorhead
What about case 4. It is typically installed on Tomcat but trying to run it locally without using a tomcat server? I tried jetty but it didn't seem to help and I would need to create profiles.Posh
I had to change the web-application-type: reactive to make it work on NETTY.Ruelle
W
45

Probably you missing @SpringBootApplication in your spring boot starter class.

@SpringBootApplication
public class LoginSecurityAppApplication {

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

}
Wuhsien answered 7/2, 2019 at 10:52 Comment(0)
W
44

The solution is:

I explicitly set the below property to none in application.yml file.

spring:
  main:
    web-application-type: none
Waft answered 9/5, 2018 at 10:17 Comment(0)
C
26

My solution had to do with a bad dependency. I had:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>

In my pom and I had to comment out the exclusion to get it working. It must look for this tomcat package for some reason.

Coucher answered 17/2, 2020 at 20:29 Comment(1)
This is exactly what the issue is in most of the cases. Tomcat is excluded so that the application can run on external servers and if the application is not extending SpringBootServletInitializer then this will occur.Spurge
A
17

Adding following bean worked for me.

    @Bean
    public ServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory();
    }

I was running non web spring application using SpringApplication.run(MyApplication.class, args); without @SpringBootApplication annotation.

Audet answered 22/8, 2020 at 8:42 Comment(0)
P
14

In my case the issue resolved on commenting the tomcat dependencies exclusion from spring-boot-starte-web

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions> -->
    </dependency> 
Physics answered 30/6, 2020 at 7:38 Comment(0)
H
13

You probably use this in your project:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

in which case you'll have to also add:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

and the magic happens :)

PS: that's because Spring will use by default web-MVC instead of web-flux when both are available

Healthy answered 24/9, 2019 at 8:32 Comment(0)
W
6

To convert an Spring boot wen application to standalone:

  1. Either configure application.properties:
spring.main.web-application-type=none
  1. Or Update application context with NONE web context.
ApplicationContext ctx = new SpringApplicationBuilder(MigrationRunner.class)
                .web(WebApplicationType.NONE).run(args);

Using application context, you can get your beans:

myBean bean = (MyBean) ctx.getBean("myBean", MyBean.class); bean.call_a_method(..);

Wilder answered 11/2, 2022 at 4:53 Comment(1)
that's the point! thanks!Solemn
S
5

Annotate class public static void main with, for example: @SpringBootApplication

Simonize answered 26/6, 2019 at 8:45 Comment(0)
S
4

As for me, I removed the provided scope in tomcat dependency.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope> // remove this scope
</dependency>
Stilbestrol answered 21/4, 2020 at 11:12 Comment(0)
C
3

I had this problem during migration to Spring Boot. I've found a advice to remove dependencies and it helped. So, I removed dependency for jsp-api Project had. Also, servlet-api dependency has to be removed as well.

compileOnly group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2' 
Chelseachelsey answered 2/1, 2019 at 20:50 Comment(2)
I got this issue with compile group: 'javax.servlet', name: 'javax.servlet-api', version:'4.0.1'Cherubini
Like @Cherubini my problem was using group: 'javax.servlet', name: 'javax.servlet-api', version:'3.1.0' with spring-boot:2.1.8.RELEASE so I updated to 'javax.servlet' to version '4.0.1' and now everything is running ok, Thanks.Leitmotiv
C
3

I did right click on my project in IntelliJ IDEA then Maven -> Reload project, problem solved.

Currey answered 11/10, 2020 at 15:57 Comment(0)
S
3

In case you're using IntelliJ and this is happening to you (like it did to my noob-self), ensure the Run setting has Spring Boot Application and NOT plain Application.

Sparse answered 30/10, 2020 at 1:44 Comment(0)
I
3

I was getting same error while using tomcat-jasper newer version

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>10.0.6</version>
</dependency>

I replaced with the stable older version it worked fine for me.

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.46</version>
</dependency>
Intermission answered 28/5, 2021 at 11:12 Comment(0)
S
2

Apart from the possible solutions in other answers, it is also possible that somehow Maven dependency corruption has occurred on your machine. I was facing the same error on trying to run my (Web) Spring boot application, and it got resolved by running the following -

mvn dependency:purge-local-repository -DreResolve=true

followed by

mvn package

I came onto this solution looking into another issue where Eclipse wouldn't let me run the main application class from the IDE, due to a different error, similar to one on this SO thread -> The type org.springframework.context.ConfigurableApplicationContext cannot be resolved. It is indirectly referenced from required .class files

Stereophotography answered 26/7, 2020 at 15:18 Comment(1)
mvn dependency:purge-local-repository -DreResolve=true; mvn clean package; It helped me.Hallucinosis
S
2

Similar to the solution of making sure org.springframework.boot:spring-boot-starter-tomcat was installed, I was missing org.eclipse.jetty:jetty-server from my build.gradle

org.springframework.boot:spring-boot-starter-web needs a server be it Tomcat, Jetty or something else - it will compile but not run without one.

Schuler answered 21/9, 2020 at 1:36 Comment(0)
F
2

I wanted to run the WAR type spring boot application, and when I was trying to run the app as spring boot application I was getting above error. So declaring the web application type in application.properties has worked for me. spring.main.web-application-type=none

Possible web application type:

  1. NONE - the application should not run as a web application and should not start an embedded web server.
  2. REACTIVE - the application should run as a reactive web application and should start an embedded reactive web server.
  3. SERVLET - the application should run as a servlet-based web application and should start an embedded servlet web server.
Faqir answered 9/5, 2021 at 15:15 Comment(0)
B
2

Missing dependency could be cause of this issue

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Boddie answered 9/12, 2021 at 15:38 Comment(0)
S
2

in my case it was annotation @SpringBootApplication missing in main

Suited answered 24/4, 2023 at 11:38 Comment(0)
P
1

In my case, the problem was I didn't had a Tomcat server separately installed in my eclipse. I assumed my Springboot will start the server automatically within itself.

Since my main class extends SpringBootServletInitializer and override configure method, I definitely need a Tomcat server installed in my IDE.

To install, first download Apachce Tomcat (version 9 in my case) and create server using Servers tab.

After installation, run the main class on server.

Run As -> Run on Server
Poon answered 28/11, 2019 at 9:37 Comment(0)
S
1

I was trying to create a web application with spring boot and I got the same error. After inspecting I found that I was missing a dependency. So, be sure to add following dependency to your pom.xml file.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency> 
Showman answered 30/4, 2020 at 12:8 Comment(0)
T
1

In my case, I was using an TOMCAT 8 and updating to TOMCAT 9 fixed it:

  <modelVersion>4.0.0</modelVersion>
  <groupId>spring-boot-app</groupId>
  <artifactId>spring-boot-app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.example.Application</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <properties>
    <tomcat.version>9.0.37</tomcat.version>
  </properties>

Related issues:

  1. https://github.com/spring-projects/spring-boot/issues?q=missing+ServletWebServerFactory+bean
  2. https://github.com/spring-projects/spring-boot/issues/22013 - Spring Boot app as a module
  3. https://github.com/spring-projects/spring-boot/issues/19141 - Application fails to load when main class extends a base class annotated with @SpringBootApplication when spring-boot-starter-web is included as a dependency
Troublesome answered 18/7, 2020 at 2:21 Comment(0)
W
1

Sometimes Intellij Behaves crazy , So If you are not able to run through Intellij, Try running project directly from command prompt.

mvn spring-boot:run
Wilmoth answered 10/11, 2023 at 4:4 Comment(0)
D
0

Upgrading spring-boot-starter-parent in pom.xml to the latest version fixed it for me.

Daiquiri answered 13/6, 2018 at 17:30 Comment(2)
can you share exactly what you added? which version?Tangency
Based on the time I posted this, it looks like it was 2.0.2.RELEASE. 2.0.3 got released the next day.Daiquiri
L
0

I encountered this problem when attempint to run my web application as a fat jar rather than from within my IDE (IntelliJ).

This is what worked for me. Simply adding a default profile to the application.properties file.

spring.profiles.active=default

You don't have to use default if you have already set up other specific profiles (dev/test/prod). But if you haven't this is necessary to run the application as a fat jar.

Leighleigha answered 23/2, 2019 at 12:52 Comment(0)
M
0

My problem was the same as that in the original question, only that I was running via Eclipse and not cmd. Tried all the solutions listed, but didn't work. The final working solution for me, however, was while running via cmd (or can be run similarly via Eclipse). Used a modified command appended with spring config from cmd:

start java -Xms512m -Xmx1024m <and the usual parameters as needed, like PrintGC etc> -Dspring.config.location=<propertiesfiles> -jar <jar>

I guess my issue was the spring configurations not being loaded correctly.

Matz answered 19/2, 2021 at 13:49 Comment(0)
L
0

In my case, the gretty plugin (3.0.6) was still active. Gretty somehow influences the embedded tomcat dependency. Removing gretty fixed the error

Lardon answered 18/3, 2022 at 16:49 Comment(0)
C
0

I had the same problem, root cause was:

<dependency>
  <groupId>org.mock-server</groupId>
  <artifactId>mockserver-netty</artifactId>
  <version>5.15.0</version>
</dependency>
<dependency>
  <groupId>org.mock-server</groupId>
  <artifactId>mockserver-client-java</artifactId>
  <version>5.15.0</version>
</dependency>

I have to define their scope as test, like <scope>test</scope>

Comical answered 14/3, 2023 at 10:52 Comment(0)
A
0

In my case, even though I had @SpringBootApplication in my main class... for some strange reason, the annotation was missing in the corresponding class in the 'target' folder. I wasted hours looking into it.

Running a Maven clean and rebuilding the app fixed the issue and the @SpringBootApplication annotation magically appeared in the corresponding main class in the target folder.

All working now, but very strange this happened in the first place.

Antiperiodic answered 16/5, 2023 at 7:6 Comment(0)
Q
0

You just need to autowire any implementation of ServletWebServerFactory interface. For example as already suggested by @Kapil it can be TomcatServletWebServerFactory or any other ones available in your app. Other possible option would be JettyServletWebServerFactory.

@Bean
public ServletWebServerFactory servletWebServerFactory() {
    return new JettyServletWebServerFactory();
}
Quarterback answered 5/7, 2023 at 0:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.