springspring-bootmaventomcattomcat8

missing org.springframework.boot.web.servlet.server.ServletWebServerFactory


I was running the program and I received this error I'm new to Spring. I used Tomcat 8-5.8. I'm using an Eclipse maven project with simple project checked

2023-09-22 15:11:02.621  INFO 12936 --- [           main] HelloWorldApp                            : Starting HelloWorldApp using Java 17.0.1 on KelvinT540 with PID 12936 (C:\tmp\myProject2\BatchTest\SpringConfig\target\classes started by Kelvin in C:\tmp\myProject2\BatchTest\SpringConfig)
2023-09-22 15:11:02.632  INFO 12936 --- [           main] HelloWorldApp                            : No active profile set, falling back to 1 default profile: "default"
2023-09-22 15:11:02.890  WARN 12936 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.context.MissingWebServerFactoryBeanException: No qualifying bean of type 'org.springframework.boot.web.servlet.server.ServletWebServerFactory' available: Unable to start AnnotationConfigServletWebServerApplicationContext due to missing ServletWebServerFactory bean
2023-09-22 15:11:02.936 ERROR 12936 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Web application could not be started as there was no org.springframework.boot.web.servlet.server.ServletWebServerFactory bean defined in the context.

Action:

Check your application's dependencies for a supported servlet web server.
Check the configured web application type.

I tried this pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.netjs</groupId>
  <artifactId>SpringBootNetjs</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringBootNetjs</name>
  <description>Spring Boot project</description>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.5</version>
  </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-tomcat</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Main file:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;

@RestController
public class HelloController {
  @GetMapping(value="/{name}")
  public String displayMessage(@PathVariable("name") String name) {
    return "Hello " + name;
  }
}

Running it:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAutoConfiguration
public class HelloWorldApp {
  public static void main(String[] args) {
    SpringApplication.run(HelloController.class, args);
  }
}

Solution

  • You need to change it to:

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

    Also, you don't need to specify @EnableAutoConfiguration explicitly. @SpringBootApplication already contains 3 annotations: @Configuration @EnableAutoConfiguration @ComponentScan

    please read this: https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.html