【问题标题】:How can I create a minimal spring-boot deployable .war file?如何创建最小的 spring-boot 可部署 .war 文件?
【发布时间】:2018-09-18 02:23:53
【问题描述】:

我正在尝试调试将现有 spring-boot 应用程序部署为 .war 文件的问题,即使在最小的情况下也无法使其工作。

特别是我:

生成的 war 文件似乎部署在具有以下上下文的 Jetty (9.4.11.v20180605) 实例中...

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/some-context-path</Set>
    <Set name="war">/some-path/gs-spring-boot-0.1.0.war</Set>
</Configure>

但是,在浏览器中访问应用程序会提供 META_INFWEB-INForg 的目录列表,而不是预期的“来自 Spring Boot 的问候!”。

我假设,也许是不公平的,我的问题不是码头设置,因为它适用于一堆其他 .war 文件,但也许我错过了我需要在码头设置的东西才能让它工作使用 spring-boot 生成的 .war 文件类型。

所以,总结一下这个问题,我缺少哪些必要步骤来生成可以部署在现有容器中的 spring-boot .war 文件?


按照上述步骤,我在项目中最终得到的三个文件如下所示,以供参考,以防有帮助。

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>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <packaging>war</packaging>

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

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

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


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

src/main/java/hello/HelloController.java

package hello;

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

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

}

src/main/java/hello/Application.java

package hello;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }

}

【问题讨论】:

  • 啊,下载最新的jetty发行版(9.4.12),将war文件复制到顶级webapps然后运行java -jar start.jar似乎工作正常,所以似乎无论问题是什么,它都是我如何配置码头容器的问题。

标签: java spring maven spring-boot jetty


【解决方案1】:

我认为我的问题是我使用的嵌入式码头不包括 org.eclipse.jetty.annotations.AnnotationConfiguration 处理器。

将一些基于https://www.eclipse.org/jetty/documentation/9.4.x/using-annotations-embedded.html 的代码添加到码头服务器对象的设置中似乎至少在这种最小情况下可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 2017-05-04
    • 1970-01-01
    • 2015-03-10
    • 2018-12-05
    相关资源
    最近更新 更多