SpringBoot - 組み込みコンテナを起動できません。

SpringBootLoginControllerクラスは、Springbootアプリケーションを起動すると、以下のようなエラー(unable to start embedded container)を発生させます(これは、Hello WorldのようなSpring Bootアプリケーションの例)。

   .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.2.RELEASE)

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]

私の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>com.test.springboot</groupId>
    <artifactId>HelloSpringBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>HelloSpringBoot</name>
    <description>HelloSpringBoot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

私のコントローラー :

import org.springframework.boot.*;
import org.springframework.web.bind.annotation.*;

@RestController
public class SpringBootLoginController {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!!!";
    }

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

}
ソリューション

SpringBootApplication**でアノテーションすることで、この問題を解決します。

@SpringBootApplication
@RestController
public class SpringBootLoginController {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!!!";
    }

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

また、@EnableAutoConfigurationを追加することでも、この問題を解決できます。

@EnableAutoConfiguration
@RestController
public class SpringBootLoginController {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!!!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootLoginController.class, args);
    }
}
解説 (0)

SpringBootLoginControllerクラスに@SpringBootApplication` アノテーションを付けてみてください。

@SpringBootApplication
@RestController
public class SpringBootLoginController {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!!!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootLoginController.class, args);
    }
}
解説 (0)

私の場合、私はスプリングブートを使用してコマンドラインプロジェクトを開発していました。

@SpringBootApplication
public class Application implements CommandLineRunner {
//my code here
}

だから私は単純なスターターを使っていました。


     org.springframework.boot
     spring-boot-starter

しかし、私もこのエラーを取得しました。私のポンには、実際に配線されたWeb関連の依存関係はありませんでした。

そしてついに、私の依存プロジェクトの1つが「javax.servlet.Servlet」を独自のポンポンで使用していることを知りました。

スプリングブーツのソースコードをチェックすると、アプリケーションの開始時にプロジェクトに「javax.servlet.Servlet」があるかどうかがチェックされます。 また、「javax.servlet.Servlet」がある場合は、Webの「埋め込みコンテナ」を開始してみてください。

「スプリングブートスターター」を使用していて、そこにWebコンテナーがなかったため、このエラーが発生しました。

したがって、解決策は非常に簡単です。これは「application.properties」のWebプロジェクトではないことをスプリングブートに伝えてください。

spring.main.web-environment=false
解説 (0)

.m2レポを削除し、バージョンを1.3.1から2.1.3に変更し、mavenをクリーンアップしてプロジェクトを再構築し、初めて実行しました(intellijを使用)。

parent>
   org.springframework.boot
   2.1.3.RELEASE
   spring-boot-starter-parent
解説 (2)

私の場合は追加します。


    javax.servlet
    servlet-api
    2.5

解決した問題。

解説 (0)

SpringBootLoginControllerクラスに注釈を付ける必要があります。 @SpringBootApplication注釈についてお読みください。

@SpringBootApplication
@RestController
public class SpringBootLoginController {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!!!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootLoginController.class, args);
    }
}
解説 (0)

mavenを使用している場合は、ビルドしてください。

mvn package

IDEの代わりにjarを構築します。

私の場合、IDEで構築されたジャーには多くの問題があります。

解説 (0)

今まで問題はわかりませんでしたが、.m2レポを削除し、インストールビルドを提供しました。

解説 (0)

の場合、依存関係に問題があります。これを追加する







        javax.servlet
        servlet-api

解説 (0)