嵌套对象列表的Spring配置属性元数据json [英] Spring configuration properties metadata json for nested list of Objects

查看:158
本文介绍了嵌套对象列表的Spring配置属性元数据json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为嵌套的对象列表配置spring配置元数据json?

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

@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

}

这是在元数据json中自动生成的

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": []
}

如何在Java代码或json中配置子属性?

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.

推荐答案

对于您的问题:如何在Java代码或json中配置子属性?"

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

详细答案:

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

特别是,请查看简单Pojo"和包装"部分.

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

简短答案:

您已经尽力了. IDE拥有所需的所有信息. NestedObject的属性可以根据输出第16行给出的信息通过反射来确定:

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>"

为IDE提供了列表可以接受的类. IDE 应该使用类名来推断NestedObject上的可用属性.但是,在撰写本文时,并不是所有的IDE都能完全反映属性和YAML格式的嵌套类.

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.

似乎IntelliJ会反映到属性文件中列表的值类型,但不会反映到映射值类型.它根本不反映到YAML文件的列表或映射值中.我不确定Spring Tool Suite,但上次检查时,它对自动完成的支持也缺少这些功能.

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.

如果您是IntelliJ用户,我建议对这两个问题进行投票,以便支持对集合类型的完全支持:

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

这篇关于嵌套对象列表的Spring配置属性元数据json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆