Spring Boot webserver works fine in Eclipse, fails to start on Server : missing EmbeddedServletContainerFactory bean

喜夏-厌秋 提交于 2019-12-21 20:00:24

问题


Following the model of the Spring Documentation I created a very simple Hello World like application. It spun right up on Eclipse and everything looked great. Sweet! I ran it and could browse to the URL. Fastest development ever.

But this has to run on a server, and looking at the jar, it was only about 4K so I knew that it wasn't going to work without a bunch of classpath configuration. To avoid that, I figured I needed a jar-with-dependencies jar.

So this is my pom.xml, basically identical to that of the Spring example except adding the jar-with-dependencies goal for the assembly plugin.

<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.whatever</groupId>
    <artifactId>LoadTestController</artifactId>
    <version>1.0.0</version>
    <name>LoadTestController</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.3</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.whatever.LoadTestController</mainClass>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

But when I go to run the thing:

java -jar LoadTestController-1.0.0-jar-with-dependencies.jar

I get:

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.

I don't think it should make any difference but here is the one (and only) class in this project:

package com.whatever;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@EnableAutoConfiguration
public class LoadTestController {

    @RequestMapping("/")
    public void simulator(HttpServletResponse response) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e1) {

        }
        response.setContentType("text/plain");
        response.setStatus(HttpServletResponse.SC_OK);
        try {
            response.getOutputStream().println("0");
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(LoadTestController.class, args);  
    }

What have I done wrong?


回答1:


And the answer is ... Don't use the maven-assembly-plugin. Reading further on in the documentation, we see that Spring has made their own plugin for this, the spring-boot-maven-plugin. Changing the pom.xml to remove the maven-assembly-plugin and use instead:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

solves the problem.

I decided to leave this question up in case anyone else runs into this problem. You think you know how to do something, but time marches on.



来源:https://stackoverflow.com/questions/34231471/spring-boot-webserver-works-fine-in-eclipse-fails-to-start-on-server-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!