如何防止Spring Boot用点解析YAML密钥 [英] How to prevent Spring Boot from parsing YAML keys with dots

查看:188
本文介绍了如何防止Spring Boot用点解析YAML密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有属性映射的YAML配置文件:

I have a YAML configuration file with a map of properties:

properties:
  a.b.c: 1

引导程序会将其解析为:

Boot will parse this as:

{a:{b:{c:1}}}

但是,我想要的是:

{'a.b.c': 1}

是否有任何办法哄骗它进入通过"键模式?引用密钥似乎无济于事.

Is there anyway to coax it into "pass through" key mode? Quoting the key doesn't seem to help.

下面的实际示例.

import static com.google.common.collect.Maps.newLinkedHashMap;

import java.util.Map;
import javax.annotation.PostConstruct;

import lombok.Data;
import lombok.val;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties("hadoop")
public class HadoopProperties {

   private Map<Object, Object> properties = newLinkedHashMap();

}

YAML

application.yml:

hadoop:
   properties:
      fs.defaultFS: hdfs://localhost:8020
      mapred.job.tracker: localhost:8021

结果

在生成的对象上调用toString():

HadoopProperties(properties = {fs = {defaultFS = hdfs://localhost:8020},mapred = {job = {tracker = localhost:8021}}})

HadoopProperties(properties={fs={defaultFS=hdfs://localhost:8020}, mapred={job={tracker=localhost:8021}}})

推荐答案

我明白了.这是因为您绑定到一个非常通用的对象,所以Spring Boot认为您的句点分隔符是映射键的取消引用.如果您绑定到地图"或属性",会怎样?

I see. It's because you are binding to a very generic object, so Spring Boot thinks your period separators are map key dereferences. What happens if you bind to Map or Properties?

这篇关于如何防止Spring Boot用点解析YAML密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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