Can I host JSON file in Spring Cloud Config Server?
We are using Spring Cloud Config Server to host all configurations for our Spring Boot applications. We want a huge JSON text to be fetched from the config server.
Our current approach is to define json text as property value
myproperty.jsontext="{'name':'value'}"
Apart from defining the JSON text as the property value, is there a way to host it and fetch it from the config server?
Does Spring Cloud Configuration Server support .json file?
Update (additional question):
Is it possible to access the searchLocations property like this?
@Value("${spring.cloud.config.server.native.searchLocations}
while keeping the following error:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.cloud.config.server.native.searchLocations' in string value "${spring.cloud.config.server.native.searchLocations}"
source to share
I've posted support for Spring Boot features at https://github.com/spring-projects/spring-boot/issues/4027#issuecomment-144649875 . Let me know if this works for you ...
source to share
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;
}
}
source to share
it is possible to serve archive files. It boils down to using an endpoint /{name}/{profile}/{label}/{path}
with {path}
which is the actual filename ... So, for example, /a-bootiful-client/default/master/myjson.json
will give you the contents of a file. However, by default, there will be no response content type application/json
, but text/html;charset=UTF-8
.
However, it will also work with "Accept: application / json":
curl -H "Accept: application/json" http://localhost:8888/a-bootiful-client/default/master/a-bootiful-client.json
{
"propName":"propValue"
}
See documentation here: http://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.3.RELEASE/single/spring-cloud-config.html#_serving_plain_text
source to share
Bypass
Since it yaml
is a subset Json
, you can store the content Json
inside the file yaml
.
- Create q config.yaml file
- Add Json content to it
- Request a file in
yaml
,Json
andproperties
, and everything will work!
I tested this approach while integrating an existing Node.js application into the Config Service where all the configs were in a file .json
. So, I just renamed them and added them to Config Repo.
source to share