时间:2022-11-12 10:13:04 | 栏目:JAVA代码 | 点击:次
在开发中,可能会遇到需要配置项目前缀的问题,虽然我们可以在Controller控制器方法中给所有请求加前缀,但是不仅比较麻烦,而且在某种环境下是没什么用处,形同虚设
接下来,教你在配置文章中只需短短一小行代码配置即可生效
在yml配置文件中加入配置:
server.servlet.context-path: /需要设置的路径前缀

再重启测试即可生效


SpringBoot自动注入实体类如下配置
配置文件:application.properties
固定前缀: sys.test.config
配置信息:
sys.test.config.industryKey=aaa sys.test.config.systemName=bbb sys.test.config.downloadUrl=ccc sys.test.config.traceDomain=ddd
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "sys.test.config")
public final class ResourceContainer {
private String industryKey;
private String systemName;
private String downloadUrl;
private String traceDomain;
public String getIndustryKey() {
return industryKey;
}
public void setIndustryKey(String industryKey) {
this.industryKey = industryKey;
}
public String getSystemName() {
return systemName;
}
public void setSystemName(String systemName) {
this.systemName = systemName;
}
public String getDownloadUrl() {
return downloadUrl;
}
public void setDownloadUrl(String downloadUrl) {
this.downloadUrl = downloadUrl;
}
public String getTraceDomain() {
return traceDomain;
}
public void setTraceDomain(String traceDomain) {
this.traceDomain = traceDomain;
}
}
@Autowired private ResourceContainer resourceContainer;