Is it possible to load spring-boot config from a .json file as opposed to .yaml or .properties? From looking at the documentation, this isn\'t supported out of the box - I\'
The spring boot way:
@EnableAutoConfiguration
@Configuration
@PropertySource(value = { "classpath:/properties/config.default.json" }, factory=SpringBootTest.JsonLoader.class )
public class SpringBootTest extends SpringBootServletInitializer {
@Bean
public Object test(Environment e) {
System.out.println(e.getProperty("test"));
return new Object();
}
public static void main(String[] args) {
SpringApplication.run(SpringBootTest.class);
}
public static class JsonLoader implements PropertySourceFactory {
@Override
public org.springframework.core.env.PropertySource> createPropertySource(String name,
EncodedResource resource) throws IOException {
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
return new MapPropertySource("json-source", readValue);
}
}
}
Define your own PropertySourceFactory and hook it in via the @PropertySource annotation. Read the resource, set the properties, use them anywhere.
Only thing is, how do you translate nested properties. The Spring way to do that (by the way you can define Json also as a variable for properties, see: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html) is to translate nested properties as such:
{"test": { "test2" : "x" } }
Becomes:
test.test2.x
Hope that helps,
Artur