Spring configuration properties metadata json for nested list of Objects

我只是一个虾纸丫 提交于 2021-02-17 19:34:06

问题


How does one configure spring configuration metadata json for 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 auto generated in the metadata json

{
  "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 do I configure the child properties either in java code or in json?

As seen below, the editor doesn't recognise the child properties.


回答1:


To your question: "How do I configure the child properties either in java code or in json?"

Long Answer:

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

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

Short Answer:

You've done all you can. The IDE has all the information it needs. The properties of NestedObject can be determined via reflection based on the information given on line 16 of your output:

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

The IDE is given the class that the list is capable of accepting. The IDE should use the class name to infer the available properties on NestedObject. However, as of this writing, not all IDEs fully reflect into the nested class for both properties and YAML formats.

It seems that IntelliJ reflects into the value type for lists in properties files, but it doesn't reflect into map value types. It doesn't reflect at all into list or map values for YAML files. I'm not sure about Spring Tool Suite, but last time I checked, its support for auto-completion was missing these features as well.

If you're an IntelliJ user, I recommend voting on these two issues so that full support for collection types will be supported:

  • https://youtrack.jetbrains.com/issue/IDEA-151708
  • https://youtrack.jetbrains.com/issue/IDEA-159276



回答2:


idea official spring-boot plugin does not support generic nest objects so far(2019.5)

workaround: install "Spring Assistant" plugin(stopped update for 1 year but still works fine together with offical plugin)




回答3:


What you have already done works fine with Spring Boot, even though the IDE does not recognise these properties. What Robert Thomton suggested is the way to go if you want to improve how the IDE works with Spring Boot configuration yaml files.

IntelliJ workaround

These yellow markers in IntelliJ are warning markers that originate from inspection (preferences -> editor -> inspections). These markers can in many cases be ignored since they are often false positives when using new features of frameworks and languages. So if they bother you enough you could disable them for just Spring Yaml files.

Further investigation

For this to work 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 the spring-configuration-metadata.json to identify the mappings used in Spring Boot Configuration yaml files. Which is in correspondence with Spring Boot Configuration meta-data documentation. The metadata file you listed does not contain entries for the properties of NestedObject. If you hover over the yellow lines and toggle quick fix, IntelliJ will suggest a fix:

IntelliJ will create this file for you:

The warnings will not immediately go away. 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 'quick fix' generated.

You can modify this additional-spring-configuration-metadata.json file to provide additional help for the IDE, such as valid values in the properties. Hopefully over time IntelliJ will be smart enough so we won't need to manually edit this file.

That did it for the original warnings, but now If you look into application.yaml you can see some new warnings:



来源:https://stackoverflow.com/questions/41417933/spring-configuration-properties-metadata-json-for-nested-list-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!