![](/img/trans.png)
[英]spring-boot request to context-path/ generates 405 Method Not Allowed error for static content
[英]How can I get context-path and info.info in a Spring-boot application?
我的应用程序是一个 spring-boot 应用程序,我启用了信息端点。 我的 application.yml 内容是:
info:
app:
name: 'app'
description: 'app desc'
version: '1.0'
server:
port: 8080
servlet:
context-path: '/app'
服务器启动时如何获取这些信息? 我想知道我的服务器的端口和上下文路径以及我的应用程序的名称等。
谢谢。
我们可以使用@Value
注解将这些值注入到 Spring 组件中。 例子:
@Value("${server.servlet.context-path}")
private String contextPath;
或者我们可以有一个配置 class,如果我们想读取文件的一部分,我们可以在其中指定一个前缀:
@Configuration
@ConfigurationProperties(prefix = "info.app")
public class AppConfig {
private String name;
private String description;
private String version;
public String getName() {
return name;
}
// Other getters omitted
}
这个 class 可以像任何其他 Spring bean 一样注入其他组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.