WAR 部署在 Tomcat 上的文件未读取 application.properties 文件
WAR file deployed on Tomcat not reading application.properties File
我使用 Java 中的 Spring 引导框架创建了一个演示 REST API。我想在 Tomcat 8 上将此 API 作为 WAR 文件部署。 API 已经配置为使用 Spring Initializr 编译为 WAR 文件。但是,部署 API 时,它不会从项目中读取 application.properties
文件。
- 我已经尝试使用
@PropertySource
和 @PropertySources
注释。
- 我也试过在 pom.xml 文件的
<build>
标签中包含 <resources>
标签。
- 我也试过将
.properties("classpath:application.properties")
添加到 ServletInitializer
没有效果
更新
我在此 post 中使用上下文路径作为示例。在我的实际应用程序中,我有一些属性存储在 application.properties 中,应用程序没有读取它们。
这是我的代码文件:
- application.properties
server.servlet.context-path=/api
- Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- ServletInitializer.java
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
- User.java
@RestController
@RequestMapping("/user")
public class User {
@GetMapping("")
public String getUser() {
return "User is here...";
}
}
点击 localhost:8080/demo/api/user
URL 应该会给出我们想要的输出,但它给出了 404 结果。然而,点击 localhost:8080/demo/user
给出了所需的输出。
这意味着应用程序没有读取 application.properties 文件,因为 api 的默认路由在该文件中设置为 /api
。
只有嵌入式 tomcat 尊重 server.servlet.context-path
。如果您要部署到外部 tomcat,请按照此处的指南设置上下文路径:https://tomcat.apache.org/tomcat-8.0-doc/config/context.html
现在,验证 application.properties
是否捆绑在 WAR
文件中。为此,请提取或打开 WAR
文件。如果未包含,则验证您的构建文件是否资源插件正在复制资源文件。
如果 application.properties
包含在 WAR 中,那么请尝试读取应用程序中的其他属性。
我使用 Java 中的 Spring 引导框架创建了一个演示 REST API。我想在 Tomcat 8 上将此 API 作为 WAR 文件部署。 API 已经配置为使用 Spring Initializr 编译为 WAR 文件。但是,部署 API 时,它不会从项目中读取 application.properties
文件。
- 我已经尝试使用
@PropertySource
和@PropertySources
注释。 - 我也试过在 pom.xml 文件的
<build>
标签中包含<resources>
标签。 - 我也试过将
.properties("classpath:application.properties")
添加到ServletInitializer
没有效果
更新
我在此 post 中使用上下文路径作为示例。在我的实际应用程序中,我有一些属性存储在 application.properties 中,应用程序没有读取它们。
这是我的代码文件:
- application.properties
server.servlet.context-path=/api
- Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- ServletInitializer.java
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
- User.java
@RestController
@RequestMapping("/user")
public class User {
@GetMapping("")
public String getUser() {
return "User is here...";
}
}
点击 localhost:8080/demo/api/user
URL 应该会给出我们想要的输出,但它给出了 404 结果。然而,点击 localhost:8080/demo/user
给出了所需的输出。
这意味着应用程序没有读取 application.properties 文件,因为 api 的默认路由在该文件中设置为 /api
。
只有嵌入式 tomcat 尊重 server.servlet.context-path
。如果您要部署到外部 tomcat,请按照此处的指南设置上下文路径:https://tomcat.apache.org/tomcat-8.0-doc/config/context.html
现在,验证 application.properties
是否捆绑在 WAR
文件中。为此,请提取或打开 WAR
文件。如果未包含,则验证您的构建文件是否资源插件正在复制资源文件。
如果 application.properties
包含在 WAR 中,那么请尝试读取应用程序中的其他属性。