简体   繁体   English

带Web套接字的Spring Boot微服务:由于缺少EmbeddedServletContainerFactory bean而无法启动EmbeddedWebApplicationContext

[英]Spring Boot Microservice w/ Web Sockets: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

What I am trying to do is write a simple Spring Boot Microservice which uses STOMP for its Web Sockets. 我想要做的是编写一个简单的Spring Boot Microservice,它将STOMP用于其Web套接字。

pom.xml: pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <artifactId>MyMicroservice</artifactId>
    <packaging>jar</packaging>

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

    <properties>
        <java.version>1.7</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency> 

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-websocket</artifactId>
        </dependency> 

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>7.0.47</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-websocket</artifactId>
            <version>7.0.90</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>

        <dependency>
           <groupId>org.apache.tomcat</groupId>
           <artifactId>tomcat-util</artifactId>
           <version>7.0.47</version>
           <scope>runtime</scope>
        </dependency>

        <dependency>
           <groupId>log4j</groupId>
           <artifactId>log4j</artifactId>
           <version>1.2.17</version>
        </dependency>
    </dependencies>

    <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <warSourceDirectory>webapp</warSourceDirectory>
                <webResources>
                    <resource>
                        <targetPath>WEB-INF/classes</targetPath>
                        <directory>src/main/resources</directory>
                        <includes>
                            <include>*.xml</include>
                            <include>*.properties</include>
                            <include>/**</include>
                        </includes>
                        <excludes>
                            <exclude>**/*.java</exclude>
                        </excludes>
                    </resource>
                    <resource>
                        <targetPath>WEB-INF</targetPath>
                        <directory>src/main/webapp/WEB-INF</directory>
                        <includes>
                            <include>*.xml</include>
                            <include>*.properties</include>
                            <include>/**</include>
                        </includes>
                        <excludes>
                            <exclude>**/*.java</exclude>
                        </excludes>
                    </resource>
                </webResources>

                </plugin>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.1</version>
            <executions>
                <execution>
                    <id>default-deploy</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
   </build>
</project>

src/main/java/ src / main / java /

package com.mymicroservice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

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

WebConfig.java WebConfig.java

package com.mymicroservice.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableWebMvc
@Configuration
@ComponentScan("com.mymicroservice")
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON);
}

WebSocketConfig.java WebSocketConfig.java

package com.mymicroservice.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }


    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket").withSockJS();
    }
}

Having an issue when trying to run my Spring Boot Microservice both like this: 尝试运行我的Spring Boot Microservice时遇到问题,如下所示:

~/mymicroservice/target$ java -jar MyMicroservice.jar

and

~/mymicroservice/mvn spring-boot:run

This is the error I receive on both: 这是我收到的两个错误:

"2018-07-10 17:42:41 - Application startup failed
"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)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
...
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

Question(s): 问题:

(1) What am I possibly doing wrong? (1)我可能做错了什么?

(2) Is there a simpler way to create a Spring Boot Microservice which uses Web Sockets? (2)有没有更简单的方法来创建使用Web套接字的Spring Boot Microservice?

(3) Is there a good example using Spring Boot 2? (3)使用Spring Boot 2有一个很好的例子吗?


Post is Edited. 帖子已编辑。

After adding the dependencies for logging and doing a: 添加用于记录的依赖项并执行以下操作后:

mvn clean install mvn全新安装

and then trying to run the executable jar file like this: 然后尝试运行可执行的jar文件,如下所示:

MyMicroservice/target$ java -jar MyMicroservice.jar
log4j:WARN No appenders could be found for logger 
(org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

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

It just brings this up and then exits (program terminates). 它只是提出来然后退出(程序终止)。

And nothing is posted in the log files. 日志文件中没有任何内容。


When running this with the -X as follows: 使用-X如下运行时:

mvn spring-boot:run -X mvn spring-boot:运行-X

Receive the following error: 收到以下错误:

[ERROR] No plugin found for prefix 'spring-boot' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/johndoe/.m2/repository), central (https://repo.maven.apache.org/maven2)] -> [Help 1]
        org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException: No plugin found for prefix 'spring-boot' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/johndoe/.m2/repository), central (https://repo.maven.apache.org/maven2)]
            at org.apache.maven.plugin.prefix.internal.DefaultPluginPrefixResolver.resolve(DefaultPluginPrefixResolver.java:93)
            at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.findPluginForPrefix(MojoDescriptorCreator.java:266)
            at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.getMojoDescriptor(MojoDescriptorCreator.java:220)
            at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegmentDefaultLifecycleTaskSegmentCalculator.java:103)
            at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegmentDefaultLifecycleTaskSegmentCalculator.java:83)
            at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:89)
            at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:309)
            at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:194)
            at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:107)
            at org.apache.maven.cli.MavenCli.execute(MavenCli.java:993)
            at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:345)
            at org.apache.maven.cli.MavenCli.main(MavenCli.java:191)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
            at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
            at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
            at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException

Scanning through your pom.xml, I think the problem is the scope of the following dependencies 扫描您的pom.xml,我认为问题在于以下依赖项的范围

<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>7.0.47</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-websocket</artifactId>
        <version>7.0.90</version>
        <scope>test</scope>
    </dependency>

Please try to remove the scope or change it to "compile". 请尝试删除范围或将其更改为“编译”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot:由于缺少 EmbeddedServletContainerFactory bean 无法启动 EmbeddedWebApplicationContext - Spring Boot: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean 由于缺少 EmbeddedServletContainerFactory bean,Spring Boot 无法启动 EmbeddedWebApplicationContext - Spring Boot Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean Spring boot -- 由于缺少 EmbeddedServletContainerFactory bean 无法启动 EmbeddedWebApplicationContext - Spring boot -- Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean 由于缺少EmbeddedServletContainerFactory bean,无法启动EmbeddedWebApplicationContext - Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean 错误:由于缺少EmbeddedServletContainerFactory bean而无法启动EmbeddedWebApplicationContext - Error : Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean Springboot可执行JAR-错误:由于缺少EmbeddedServletContainerFactory bean而无法启动EmbeddedWebApplicationContext - Springboot Executable JAR - Error: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean Spring Boot异常-由于缺少EmbeddedServletContainerFactory bean - Spring boot exception - due to missing EmbeddedServletContainerFactory bean 由于缺少EmbeddedServletContainerFactory bean,Spring Application无法启动 - Spring Application doesn't start due to missing EmbeddedServletContainerFactory bean spring-boot web 应用程序启动失败:由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext - spring-boot web app fails to start : Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Jetty 9 + Spring 引导 - 由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext - Jetty 9 + Spring Boot - Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM