Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何使用key和null值在yml中定义map?_Spring Boot_Yaml - Fatal编程技术网

Spring boot 如何使用key和null值在yml中定义map?

Spring boot 如何使用key和null值在yml中定义map?,spring-boot,yaml,Spring Boot,Yaml,我要求在yml属性中使用映射,但允许在映射中输入带有null值的键 例如: application.yml .... init-services: sample-service-discovery: port: 8761 profiles: dev,prod sample-config-server: port: 8888 profiles: dev, test sample-activation-service: some-other-servi

我要求在yml属性中使用
映射
,但允许在映射中输入带有null值的键

例如:

application.yml

....
init-services:
  sample-service-discovery:
    port: 8761
    profiles: dev,prod
  sample-config-server:
    port: 8888
    profiles: dev, test
  sample-activation-service:
  some-other-service:
    port: 1234
....
init services
是一个
Map

ServiceProperties
只是一个带有
整数端口、列表配置文件的POJO

对于前2个条目,它可以很好地编译,但是在
示例激活服务
上,我得到
无法将类型“java.lang.String”的值转换为所需的类型PathProperties$ServiceProperties
异常


问题:有没有办法允许在.yml映射中有一个只有键但null值为null的条目?以后我需要迭代这些键,即使它们的值为空。

我通过向
ServiceProperties
添加一个构造函数来解决这个问题,该构造函数将字符串作为参数,并调用默认构造函数

public ServiceProperties(String empty) {this();}

您可以有一个
null
YAML的键,但这使它成为非字符串标量。你确定你想要“字符串键带空值”在我看来像是一个矛盾的术语。我不想要空键,我想要一个空值的键。因此,如果我在代码中执行:
map.get(“示例激活服务”)
它将返回
null
。如果我的措辞不够清晰,我愿意接受建议/编辑。我编辑了这个问题,试图使它更清楚。如果有帮助,请告诉我。对不起,我不知道如何使用Java生成。但是
{null:1,True:x,x:42}
是有效的YAML。当然,在任何映射中只能有一个
null
键,因为映射的键必须是唯一的。正如我在前面的评论中所说的,我正在寻找一个null值,而不是null键。示例激活服务:
在我的yaml文件中是一个键,它应该有一个对象作为一个值,但我希望该值为空或null。目前,至少在SpringBoot中,这是行不通的。如果我问题上的yaml标签有误导性,我会删除它