简体   繁体   中英

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 retrieved from the Config Server.

Our current approach is to define the json text as a property value

myproperty.jsontext="{'name':'value'}"

Apart from defining the JSON text as a property value, is there any way to host & fetch it from the config server ?

Does Spring Cloud Config Server support a .json file ?

Update (additional Question):

Can i access the searchLocations property as follows ?

@Value("${spring.cloud.config.server.native.searchLocations}

while acessing, we keep getting 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}"

I have submitted the feature support to Spring Boot at https://github.com/spring-projects/spring-boot/issues/4027#issuecomment-144649875 . Let me know if that works for you...

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;
    }

}

Workaround

Since yaml is a subset of Json , you can store a Json content inside of a Yaml file.

  • Create q file config.yaml
  • Add the Json content in it
  • Request the file in yaml , json and properties and everything works!

I have tested this approach when integrating an existing Node.js app to Config Service where all the configs were in .json file. So, I just renamed all of them and added to a Config Repo.

there's the possibility to serve arbitary files. It boils down to using the endpoint /{name}/{profile}/{label}/{path} with {path} being the actual filename... So eg /a-bootiful-client/default/master/myjson.json will give you the file contents. However by default the content-type of the response will not be 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 the documentation here: http://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.3.RELEASE/single/spring-cloud-config.html#_serving_plain_text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM