I want to read all the values and its key from application.properties file in Java spring boot application.
I can across this article which read values in multiple ways. The problem with this approach is every time you add a property, we need to update the code.
I want a dynamic way to read all keys and values. I do not want to make code changes once a property is added to application.properties file.
I tried something like this and did not got the desired result:
@Autowired
private Environment environment;
@GetMapping
public Map<String, String> getAllProperties() {
Map<String, String> properties = new HashMap<>();
// Get all property names
String[] propertyNames = environment.getProperty("spring.config.name", "application").split(",");
for (String propertyName : propertyNames) {
String propertyValue = environment.getProperty(propertyName);
properties.put(propertyName, propertyValue != null ? propertyValue : "null");
}
return properties;
}
I checked and application.properties file is located under src/main/resources & the file name is application.properties
Let me know if what I am trying to achieve is possible or not.
your properties are in resources, writing to resources is a bad idea
Of course, you can write a separate method that will read from the properties file, but you will have problems when you build your project in war or jar
Create a hidden folder and put whatever you want there