gpt4 book ai didi

java - 嵌套对象列表的 Spring 配置属性元数据 json

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:04:52 25 4
gpt4 key购买 nike

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

场景

@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 中自动生成的内容

{
"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 中配置子属性?

如下所示,编辑器无法识别子属性。

enter image description here

最佳答案

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

长答案:

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

请特别注意“Simple Pojo”和“Wrapping Up”部分。

简答:

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

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

IDE 被赋予列表能够接受的类。 IDE 应该 使用类名来推断 NestedObject 上的可用属性。但是,截至撰写本文时,并非所有 IDE 都将属性和 YAML 格式都完全反射(reflect)到嵌套类中。

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

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

关于java - 嵌套对象列表的 Spring 配置属性元数据 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41417933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com