代码之家  ›  专栏  ›  技术社区  ›  Matt Sheppard

如何创建一个最小的spring boot deployable.war文件?

  •  1
  • Matt Sheppard  · 技术社区  · 6 年前

    具体来说,我:

    生成的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_INF , WEB-INF org 而不是期待中的“春天的问候!”。

    我假设,也许不公平,我的问题不在于jetty设置,因为它对一堆其他.war文件运行良好,但也许我遗漏了一些我需要在jetty中设置的东西,以便它与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控制器.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!";
        }
    
    }
    

    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);
                }
    
            };
        }
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Matt Sheppard    6 年前

    我想我的问题是我使用的嵌入式防波堤不包括org.eclipse.jetty网站.annotations.AnnotationConfiguration配置处理器。

    添加一些基于 https://www.eclipse.org/jetty/documentation/9.4.x/using-annotations-embedded.html 对jetty服务器对象的设置来说,至少在这种最小的情况下,它似乎可以工作。