在SpringBoot项目中实现给所有请求加固定前缀
目录
- 给所有请求加个固定前缀
- 知识小锦囊
- 配置文件读取(固定前缀)
- 1.配置文件所有固定前缀的都可以使用
- 2.SpringBoot实体类
- 3.使用方式
给所有请求加个固定前缀
在开发中,可能会遇到需要配置项目前缀的问题,虽然我们可以在Controller控制器方法中给所有请求加前缀,但是不仅比较麻烦,而且在某种环境下是没什么用处,形同虚设
接下来,教你在配置文章中只需短短一小行代码配置即可生效
知识小锦囊
在yml配置文件中加入配置:
server.servlet.context-path: /需要设置的路径前缀

再重启测试即可生效


配置文件读取(固定前缀)
1.配置文件所有固定前缀的都可以使用
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
2.SpringBoot实体类
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;
}
}
3.使用方式
@Autowired private ResourceContainer resourceContainer;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)
