Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot missing ServletWebServerFactory bean

I keep getting this error:

Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

This is the target in my run configuration.

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);
    }

}

And this is MyApp.class:

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";
    }
}

And this is my Gradle build file:

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
}

Both MyApp.java and Runner.java are in the same package, "mystuff". I searched around here on Stackoverflow, but my configuration appears to be correct.

like image 781
Beebunny Avatar asked May 10 '26 10:05

Beebunny


1 Answers

In Runner class, replace

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

with

new SpringApplicationBuilder(MyApp.class).web(WebApplicationType.SERVLET).run(args);
like image 130
narendra-choudhary Avatar answered May 12 '26 06:05

narendra-choudhary