我可以在Spring Cloud Config Server中托管JSON文件吗?

时间:2015-07-29 11:10:32

标签: java json spring config spring-cloud

我们正在使用Spring Cloud Config Server来托管Spring Boot应用程序的所有配置。我们希望从Config Server中检索一个巨大的JSON文本。

我们当前的方法是将json文本定义为属性值

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

除了将JSON文本定义为属性值之外,还有什么方法可以托管&从配置服务器获取它?

Spring Cloud Config Server是否支持.json文件?

更新(附加问题):

我可以按如下方式访问searchLocations属性吗?

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

在进行访问时,我们不断收到以下错误

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.cloud.config.server.native.searchLocations' in string value "${spring.cloud.config.server.native.searchLocations}"

4 个答案:

答案 0 :(得分:2)

我已在https://github.com/spring-projects/spring-boot/issues/4027#issuecomment-144649875向Spring Boot提交了功能支持。如果这对您有用,请告诉我......

答案 1 :(得分:1)

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.

答案 2 :(得分:0)

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 :(得分:0)

有可能提供任意档案。归结为使用端点/{name}/{profile}/{label}/{path},其中{path}是实际的文件名...例如, /a-bootiful-client/default/master/myjson.json将为您提供文件内容。但是,默认情况下,响应的内容类型不是application/json,而是text/html;charset=UTF-8

然而,它也适用于“Accept:application / json”:

curl -H "Accept: application/json" http://localhost:8888/a-bootiful-client/default/master/a-bootiful-client.json
{
        "propName":"propValue"
}

请参阅此处的文档:http://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.3.RELEASE/single/spring-cloud-config.html#_serving_plain_text