我正在尝试调试将现有 spring-boot 应用程序部署为 .war 文件的问题,但即使在最小的情况下也无法使其工作。
具体来说,我:
- 已关注 https://spring.io/guides/gs/spring-boot/直到我可以成功运行 jar 文件并使用curl 连接到它。
- 已关注 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file要添加 SpringBootServletInitializer,请将包装更改为 .war 并按提供方式标记 spring-boot-starter-tomcat。
生成的 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
的目录列表,而不是预期的“来自 Spring Boot 的问候!”。
我假设,也许不公平,我的问题不是jetty设置,因为它对于一堆其他.war文件工作得很好,但也许我缺少一些我需要在Jetty中设置的东西,以使其与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不包括org.eclipse.jetty.annotations.AnnotationConfiguration处理器。
根据https://www.eclipse.org/jetty/documentation/9.4.x/using-annotations-embedded.html添加一些代码Jetty 服务器对象的设置看起来至少在这个最小的情况下它可以工作。
关于java - 如何创建最小的 spring-boot 可部署 .war 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52378029/