lnunno lnunno - 6 months ago 51
JSON Question

Accessing Spring Configuration Properties Metadata through code API

I recently learned that it is possible for Spring to generate the

file and also create a

Is it possible to access this information through Spring natively and, for example, be able to print help messages out that are extracted from this JSON file? I did not see anything like that mentioned in the Spring documentation. As far as I can tell, configuration classes are POJOs that do not have any Spring metadata associated with them in the actual injected objects.

Say I have the following class:

package test;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "myConfig")
public class MyConfig
* myField description.
private int myField = 3;

public int setMyField(int i){ myField = i; }
public int getMyField(){ return myField; }

And this will generate the following metadata:

"groups": [{
"name": "myConfig",
"type": "test.MyConfig",
"sourceType": "test.MyConfig"
"properties": [
"name": "myConfig",
"type": "java.lang.Integer",
"description": "myField description.",
"sourceType": "test.MyConfig",
"defaultValue": 3
"hints": []

Ideally I would have some way to write
and have that return
"myField description"
. I would also like to be able to add additional metadata in the JSON and retrieve them a similar way.


Spring Boot ships with a separate module that does just that. It's used internally for tools that validate the consistency of the metadata and this is also the base of the STS support.

Check the spring-boot-configuration-metadata project.

If you add this to your project, you can easily build a repository of keys defined in the current classpath:

Resource[] resources = new PathMatchingResourcePatternResolver()
ConfigurationMetadataRepositoryJsonBuilder builder = ConfigurationMetadataRepositoryJsonBuilder.create();
for (Resource resource : resources) {
    try (InputStream in = resource.getInputStream()) {
ConfigurationMetadataRepository repository = builder.build();