从 JSON 属性文件读取时,ReloadableConfiguration getProperty 返回 null 而不是 map

问题描述 投票:0回答:1

我需要一个 ReloadableConfiguration 来处理 JSON 文件以从属性文件中读取属性。我尝试过的所有方法都适用于字符串或列表,但不适用于映射。它只是返回 null 而不是

Map<String, String>
对象(或其他任何内容)(请参阅下面示例测试用例中“foo”的值)。

以下测试用例总结了迄今为止实现的所有内容。

configuration.getProperty("foo")
的返回值为null。我还尝试使用
getCollection
而不是
getProperty
和其他一些功能,但它也无法检索地图:

    @Test
    void testJsonConfigLoading() throws IOException {
        // Create property file
        Path configurationFile = Files.createTempFile("json-test-foo", ".properties");
        String TEST_FILE_DIR = "my.test.dir";
        System.setProperty(TEST_FILE_DIR, configurationFile.getParent().toString());
        String prop = "{\"bla\": \"blubb\", \"foo\": {\"k1\": \"A\", \"k2\": \"B\"}}";
        Files.write(configurationFile, prop.getBytes(), StandardOpenOption.WRITE);

        // Create property reader
        Parameters params = new Parameters();
        ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder = 
                new ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration>(JSONConfiguration.class)
                .configure(
                        params
                                .fileBased()
                                .setFile(configurationFile.toFile())
                );
        ImmutableConfiguration configuration = new ReloadableConfiguration(builder);

        // test simple string
        String bla = configuration.getString("bla");
        assertEquals("blubb", bla);

        // test map
        Object property = configuration.getProperty("foo");
        assertNotNull(property); // <= here the test case fails since property is null
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(property);
        Map<String, String> foo = mapper.readValue(json, Map.class);
        Map<String, String> map1 = Stream.of(new String[][] {
                { "k1", "A" },
                { "k2", "B" },
        }).collect(Collectors.toMap(data -> data[0], data -> data[1]));
        assertEquals(map1, foo);
    }

我尝试了 ConfigurationBuilder 的不同配置参数和不同的方法来从配置中获取一些信息。但是

configuration.getProperty("foo")
(或其他 getter)的返回值仍然为空,而不是预期的
Map<String, String>

是的,当然,我在 https://commons.apache.org/proper/commons-configuration/userguide/user_guide.html 搜索/阅读了用户指南,但没有找到任何对我有进一步帮助的内容。有什么想法吗?

java properties-file
1个回答
0
投票

最后,我用以下几行代码替换了上面代码中的代码:

ImmutableConfiguration subset = configuration.immutableSubset("foo");
assertNotNull(subset);
Iterator<String> keysIterator = subset.getKeys();
Map<String, String> propertyMap = stream(
            Spliterators.spliteratorUnknownSize(keysIterator, Spliterator.ORDERED), false)
        .collect(Collectors.toMap(Function.identity(), s -> subset.getString(s)));
Map<String, String> map1 = Stream.of(new String[][] {
            { "k1", "A" },
            { "k2", "B" },
    }).collect(Collectors.toMap(data -> data[0], data -> data[1]));
assertEquals(map1, propertyMap);
© www.soinside.com 2019 - 2024. All rights reserved.