yat*_*gan 6 java spring json config spring-cloud
我们使用 Spring Cloud Config Server 来托管 Spring Boot 应用程序的所有配置。我们希望从配置服务器中检索一个巨大的 JSON 文本。
我们目前的做法是将json文本定义为一个属性值
myproperty.jsontext="{'name':'value'}"
Run Code Online (Sandbox Code Playgroud)
除了将 JSON 文本定义为属性值之外,还有什么方法可以从配置服务器托管和获取它?
Spring Cloud Config Server 是否支持 .json 文件?
更新(附加问题):
我可以按如下方式访问 searchLocations 属性吗?
@Value("${spring.cloud.config.server.native.searchLocations}
Run Code Online (Sandbox Code Playgroud)
在访问时,我们不断收到以下错误
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.cloud.config.server.native.searchLocations' in string value "${spring.cloud.config.server.native.searchLocations}"
Run Code Online (Sandbox Code Playgroud)
小智 1
public class JsonPropertySourceLoader implements PropertySourceLoader {
private static final String JSON = "json";
@Override
public final String[] getFileExtensions() {
return new String[] { JSON };
}
@Override
public PropertySource<?> load(final String name,
final Resource resource, final String profile) {
final Map<String, Object> source = process(resource);
return new MapPropertySource(name, source);
}
@SuppressWarnings("unchecked")
private Map<String, Object> process(final Resource resource) {
Map<String, Object> map = null;
try {
map = new ObjectMapper()
.readValue(resource.getFile(), LinkedHashMap.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
return map;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7910 次 |
| 最近记录: |