Spring json metadata properties properties for nested object list

How to configure spring json configuration metadata for a nested list of objects?

Scenario

@ConfigurationProperties(prefix = "custom-config") public class ConfigProperties { private boolean booleanProperty; private List<NestedObject> listProperty = new LinkedList<>(); //getters and setters } public class NestedObject { private String stringProperty; private boolean booleanProperty; //getters and setters } 

This is what was created automatically in json metadata

 { "groups": [{ "name": "custom-config", "type": "testing.config.properties.ConfigProperties", "sourceType": "testing.config.properties.ConfigProperties" }], "properties": [ { "name": "custom-config.boolean-property", "type": "java.lang.Boolean", "sourceType": "testing.config.properties.ConfigProperties", "defaultValue": false }, { "name": "custom-config.list-property", "type": "java.util.List<testing.config.properties.NestedObject>", "sourceType": "testing.config.properties.ConfigProperties" } ], "hints": [] } 

How to configure child properties in both java code and json?

As you can see below, the editor does not recognize child properties.

enter image description here

+6
source share
3 answers

To your question: "How to configure child properties in both java code and json?"

Long answer:

See https://github.com/spring-projects/spring-boot/wiki/IDE-binding-features#simple-pojo

In particular, see the sections "Simple Pojo" and "Wrapping Up".

Short answer:

You did everything you could. The IDE has all the necessary information. NestedObject properties can be defined by reflection based on the information indicated on line 16 of your output:

 "type": "java.util.List<testing.config.properties.NestedObject>" 

The IDE provides a class that a list can accept. The IDE should use the class name to display the available properties on a NestedObject . However, at the time of this writing, not all IDEs are fully reflected in the nested class for both properties and YAML formats.

IntelliJ seems to reflect the value type for lists in property files, but does not reflect it in map value types. It does not at all reflect in the values ​​of the list or map for YAML files. I'm not sure about the Spring Tool Suite, but the last time I checked, its support for automatic completion was also missing.

If you are an IntelliJ user, I recommend voting on these two issues in order to support support for collection type support:

+8
source

What you have already done works fine with Spring Boot, although the IDE does not recognize these properties. What Robert Tomton suggested is the way to go if you want to improve the IDE's work with yaml Spring Boot configuration files.

IntelliJ workaround

These yellow markers in IntelliJ are warning markers that come from validation (preferences β†’ editor β†’ validations). In many cases, these markers can be ignored, as they are often false positives when using the new functions of frameworks and languages. Therefore, if they bother you, you can disable them only for Yaml files.

Further research

For this you need to have spring-boot-configuration-processor as a dependency

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> 

IntelliJ seems to use spring-configuration-metadata.json to identify the mappings used in the yaml Spring Boot Configuration files. This corresponds to the Spring Boot Configuration Metadata Documentation . The metadata file you listed does not contain entries for the NestedObject properties. If you hover over the yellow lines and go to the quick fix, IntelliJ will offer a fix:

quick fix

IntelliJ will create this file for you: additional-spring-configuration-metadata.json

Warnings do not immediately disappear. But if you do a clean build (depending on your build tool), they will go away.

If you now open target / META-INF / spring-configuration-metadata.json, you can see that Spring Boot has added the contents of the additional spring-configuration-metadata.json that the previous one generates a "quick fix".

You can modify this additional spring-configuration-metadata.json file to provide additional information for the IDE, such as valid property values. We hope that with time IntelliJ will be smart enough, so we do not need to manually edit this file.

This was done for the initial warnings, but now, if you look at application.yaml, you can see some new warnings:

Duplicate configuration key

0
source

The idea of ​​the official Spring- Boot plug-in does not yet support the universal socket object (2019.5) Workaround : Install the "Spring Assistant" plug-in (stopped updating for 1 year, but still works fine with the official plug-in)

0
source

Source: https://habr.com/ru/post/1013687/


All Articles