SpringBoot缺少Servlet网络服务器工厂bean

ecbunoof  于 2023-02-04  发布在  Spring
关注(0)|答案(3)|浏览(160)

我一直收到这个错误:
原因:org. springframework.上下文。应用程序上下文异常:由于缺少Servlet Web服务器工厂Bean,无法启动Servlet Web服务器应用程序上下文。
这是我运行配置中的目标。

package mystuff;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication(scanBasePackages = "mystuff")
public class Runner {

    public static void main(String[] args) {
        new SpringApplicationBuilder(MyApp.class).run(args);
    }

}

这是我的应用程序类:

package mystuff;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyApp {

    @RequestMapping("/")
    public String index() {
        return "Hello World";
    }
}

这是我的Gradle构建文件:

plugins {
    java
    id("org.springframework.boot") version "2.2.2.RELEASE"
    id("io.spring.dependency-management") version "1.0.8.RELEASE"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit", "junit", "4.12")
}

configure<JavaPluginConvention> {
    sourceCompatibility = JavaVersion.VERSION_1_8
}

www.example.com和www.example.com都在同一个软件包"mystuff"中。我在stackoverflow上搜索了一下,但我的配置似乎是正确的。 MyApp.java and Runner.java are in the same package, "mystuff". I searched around here on Stackoverflow, but my configuration appears to be correct.

58wvjzkj

58wvjzkj1#

你能不能试着

@SpringBootApplication(scanBasePackages = "mystuff")
public class Runner {

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

}
bqucvtff

bqucvtff2#

Runner类中,将

new SpringApplicationBuilder(MyApp.class).run(args);

new SpringApplicationBuilder(MyApp.class).web(WebApplicationType.SERVLET).run(args);
lymgl2op

lymgl2op3#

.build()方法应在.run()之前调用
请尝试以下代码

new SpringApplicationBuilder(MyApp.class).build().run(args);

相关问题