使用下划线解析@ConfigurationProperties中的嵌套Map

时间:2018-02-20 09:53:48

标签: java spring spring-boot properties

使用spring-boot 1.4.6,我正在尝试使用下划线和驼峰大小写而不是连字符和句点来配置自定义属性。这是因为我们将属性作为系统变量传递,并且使用的shell不允许使用连字符或变量名称的句点。这是属性bean(由lombok创建的getters / setter):

@Data
@Accessors(prefix = "")
@ConfigurationProperties("image.variations")
public class ImageVariationsProperties {

    private Map<String, VariationsHost> hosts;

    @Data
    public static class VariationsHost {            

        private Map<String, Variation> variations;
    }

    @Data
    public static class Variation {

        private int maxWidth;
        private int maxHeight;

    }

}

这一切都适用于

等配置
image.variations.hosts:
  default:
    variations:
      small:
        max-width: 360
        max-height: 220
      medium:
        max-width: 750
        max-height: 450
      large:
        max-width: 1350
        max-height: 900

但是打破例如用

替换最后一项
image_variations_hosts_default_variations_large_maxHeight: 900

显示此错误

Binding to target ImageVariationsProperties(hosts={default=ImageVariationsProperties.VariationsHost(variations={small=ImageVariationsProperties.Variation(maxWidth=360, maxHeight=220), medium=ImageVariationsProperties.Variation(maxWidth=750, maxHeight=450), large=ImageVariationsProperties.Variation(maxWidth=1350, maxHeight=0)})}) failed:

    Property: image.variations.hosts[default_variations_large_maxHeight]
    Value: 900
    Reason: Failed to convert property value of type 'java.lang.Integer' to required type 'com.acme.ImageVariationsProperties$VariationsHost' for property 'hosts[default_variations_large_maxHeight]'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Integer' to required type 'com.acme.ImageVariationsProperties$VariationsHost' for property 'hosts[default_variations_large_maxHeight]': no matching editors or conversion strategy found

它显然只能解析第一个地图值,但这对于嵌套地图也不应该起作用吗?

请注意,由于上述原因,我需要使用下划线,不能使用连字符,句号,括号等。

1 个答案:

答案 0 :(得分:0)

您是否尝试过全部大写的价值? IMAGE_VARIATIONS_HOSTS_DEFAULT_VARIATIONS_LARGE_MAXHEIGHT 应该运作,this

您还可以在more general页面中找到有关配置绑定的更多相关信息。