【问题标题】:Missing ServletWebServerFactory Spring Boot Bean缺少 ServletWebServerFactory Spring Boot Bean
【发布时间】:2023-03-31 04:34:02
【问题描述】:

我目前正在学习 Spring Boot,但我有一个关于服务器运行的小问题 下面是代码

我的 springJPA 应用程序类

@Configuration
public class SpringJpaApplication {

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

}

这是我的控制器*

@Controller
public class IssuesController {
    
    @Autowired
    IssueRepository repo; 
    
    @RequestMapping("/home")
    public String home() {
        return "home.jsp"; 
}
    
    @RequestMapping("/addIssue")
    public ModelAndView addIssue(Issues issue) {
        ModelAndView mv = new ModelAndView(); 
        
        mv.addObject("obj", issue); 
        mv.setViewName("home");
        
        repo.save(issue);
        return mv; 
    }
    
}

这是我制作的应用程序属性,以便能够在我的演示应用程序中使用 SQLite

spring.jpa.hibernate.ddl-auto=update
 
 
spring.datasource.url = jdbc:sqlite:sqlitesample.db
spring.datasource.driver-class-name = org.sqlite.JDBC
 
spring.datasource.username = admin
spring.datasource.password = admin
spring.sql.init.mode = always

最后,这些是我添加的一些依赖项

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demoApplication</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springJPA</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
    
        <dependency>
             <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
        
        <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        </dependency>
        
        <dependency>
     <groupId>org.apache.tomcat</groupId>
     <artifactId>tomcat-jasper</artifactId>
     <version>9.0.48</version>
     </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

但是,当我尝试运行应用程序时,我不断收到此错误

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163) ~[spring-boot-2.5.1.jar:2.5.1]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) ~[spring-context-5.3.8.jar:5.3.8]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.1.jar:2.5.1]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.1.jar:2.5.1]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.1.jar:2.5.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.1.jar:2.5.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.1.jar:2.5.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.1.jar:2.5.1]
    at com.example.demo.SpringJpaApplication.main(SpringJpaApplication.java:11) ~[classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:210) ~[spring-boot-2.5.1.jar:2.5.1]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) ~[spring-boot-2.5.1.jar:2.5.1]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160) ~[spring-boot-2.5.1.jar:2.5.1]
    ... 8 common frames omitted

我错过了什么?

【问题讨论】:

    标签: java spring-boot sqlite hibernate


    【解决方案1】:

    参考ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean,好像你缺少注释@SpringBootApplication

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

    【讨论】:

    • 我尝试按照您的建议进行操作,但现在出现新错误。这次是应用程序上下文错误。
    • 那么请发布整个错误和代码。此错误可能与缺少 Spring Annotations 或错误的 jdbc 设置或许多其他问题有关。
    猜你喜欢
    • 2020-08-08
    • 2019-08-07
    • 2018-03-09
    • 2019-12-03
    • 2018-12-16
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    相关资源
    最近更新 更多