Spring configuration properties metadata json for nested list of Objects

后端 未结 3 1297
醉梦人生
醉梦人生 2021-02-05 23:47

How does one configure spring configuration metadata json for nested list of objects?

Scenario

@ConfigurationProperties(prefix = \"custom-config\")
pub         


        
相关标签:
3条回答
  • 2021-02-05 23:51

    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
    0 讨论(0)
  • 2021-02-05 23:52

    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)

    0 讨论(0)
  • 2021-02-06 00:13

    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:

    0 讨论(0)
提交回复
热议问题