之前的解决方案假设您的 spring.cloud.config.server.git.uri 是 https 类型。下面的解决方案将引用包含已从 repo 中提取的文件的本地文件系统,并且适用于任何 repo 类型。端点还接受搜索路径并采用以下格式:
-
/{name}/{profile}/{label}/{path}/listFiles
/{name}/{profile}/{path}/listFiles?useDefaultLabel
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
@RestController
public class CloudConfigApplication {
private UrlPathHelper helper = new UrlPathHelper();
private SearchPathLocator service;
public static void main(String[] args) {
SpringApplication.run(CloudConfigApplication.class, args);
}
public CloudConfigApplication(SearchPathLocator service) {
this.service = service;
}
@RequestMapping("/{name}/{profile}/{label}/**/listFiles")
public List<String> retrieve(@PathVariable String name, @PathVariable String profile,
@PathVariable String label, ServletWebRequest request,
@RequestParam(defaultValue = "true") boolean resolvePlaceholders)
throws IOException {
String path = getDirPath(request, name, profile, label);
return listAll(request, name, profile, label, path);
}
@RequestMapping(value = "/{name}/{profile}/**/listFiles", params = "useDefaultLabel")
public List<String> retrieve(@PathVariable String name, @PathVariable String profile,
ServletWebRequest request,
@RequestParam(defaultValue = "true") boolean resolvePlaceholders)
throws IOException {
String path = getDirPath(request, name, profile, null);
return listAll(request, name, profile, null, path);
}
private String getDirPath(ServletWebRequest request, String name, String profile, String label) {
String stem;
if (label != null) {
stem = String.format("/%s/%s/%s/", name, profile, label);
} else {
stem = String.format("/%s/%s/", name, profile);
}
String path = this.helper.getPathWithinApplication(request.getRequest());
path = path.substring(path.indexOf(stem) + stem.length()).replace("listFiles", "");
return path;
}
public synchronized List<String> listAll(ServletWebRequest request, String application, String profile, String label, String path) {
if (StringUtils.hasText(path)) {
String[] locations = this.service.getLocations(application, profile, label).getLocations();
List<String> fileURIs = new ArrayList<>();
try {
int i = locations.length;
while (i-- > 0) {
String location = String.format("%s%s", locations[i], path).replace("file:", "");
Path filePath = new File(location).toPath();
if(Files.exists(filePath)) {
fileURIs.addAll(Files.list(filePath).filter(file -> !Files.isDirectory(file)).map(file -> {
String URL =
String.format(
"%s://%s:%d%s/%s/%s/%s%s?useDefaultLabel",
request.getRequest().getScheme(), request.getRequest().getServerName(),
request.getRequest().getServerPort(), request.getRequest().getContextPath(),
application, profile, path,
file.getFileName().toString());
if(label != null) {
URL = String.format(
"%s://%s:%d%s/%s/%s/%s/%s%s",
request.getRequest().getScheme(), request.getRequest().getServerName(),
request.getRequest().getServerPort(), request.getRequest().getContextPath(),
application, profile, label, path,
file.getFileName().toString());
}
return URL;
}).collect(Collectors.toList()));
}
}
} catch (IOException var11) {
throw new NoSuchResourceException("Error : " + path + ". (" + var11.getMessage() + ")");
}
return fileURIs;
}
throw new NoSuchResourceException("Not found: " + path);
}
}
获取列表:
curl http://localhost:8080/context/appName/profile/label/some/path/listFiles
示例结果:
[
"http://localhost:8080/context/appName/profile/label/some/path/robots.txt"
]