Can I host JSON file in Spring Cloud Config Server?

我是研究僧i 提交于 2019-12-13 13:20:12

问题


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

回答1:


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...




回答2:


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

}



回答3:


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.




回答4:


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 e.g. /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



来源:https://stackoverflow.com/questions/31698783/can-i-host-json-file-in-spring-cloud-config-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!