我浏览了链接:如何多次传递带有application.properties和其他相关链接的Map<String,String>,但仍然不起作用。
我正在使用Spring Boot和Spring REST示例。链接问题:默认情况下如何在 Spring Boot REST 中执行最新版本的端点?
我已经创建了这样的映射,只需阅读映射
get.customers={GET: '/app-data/customers', VERSION: 'v1'}
post.customers={POST: '/app-data/customers', VERSION: 'v1'}
get.customers.custId={GET: '/app-data/customers/{custId}', VERSION: 'v2'}
法典:
private String resolveLastVersion() {
// read from configuration or something
return "2";
}
法典:
@Component
@ConfigurationProperties
@PropertySource("classpath:restendpoint.properties")
public class PriorityProcessor {
private final Map<String, String> priorityMap = new HashMap<>();
public Map<String, String> getPriority() {
return priorityMap;
}
}
法典:
我建议以下实现:
@ConfigurationProperties(prefix="request")
public class ConfigurationProps {
private List<Mapping> mapping;
public List<Mapping> getMapping() {
return mapping;
}
public void setMapping(List<Mapping> mapping) {
this.mapping = mapping;
}
}
类映射将表示有关单个映射的信息:
public class Mapping {
private String method;
private String url;
private String version;
public Mapping(String method, String url, String version) {
this.method = method;
this.url = url;
this.version = version;
}
public Mapping() {
}
// getters setters here
}
在配置或 Spring 引导应用程序类(带有 main 方法的类(上:@EnableConfigurationProperties(ConfigurationProps.class)
在属性文件中放置:
request.mapping[0].method=get
request.mapping[0].url=/customers
request.mapping[0].version=1
request.mapping[1].method=post
request.mapping[1].url=/students
request.mapping[1].version=2
在过滤器中(我假设您遵循了我从链接问题中的建议(:
@Component
@Order(1)
public class LatestVersionFilter implements Filter {
private List<Mapping> mappings;
public LatestVersionFilter(ConfigurationProps props) {
this.mappings = props.getMapping();
}
}