How to prevent Spring Boot from parsing YAML keys with dots

时光总嘲笑我的痴心妄想 提交于 2019-12-10 15:16:27

问题


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

However, what I desire is :

{'a.b.c': 1}

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


Update

Actual example below.

Java

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

Result

Calling toString() on the resulting object:

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


回答1:


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?



来源:https://stackoverflow.com/questions/23350778/how-to-prevent-spring-boot-from-parsing-yaml-keys-with-dots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!