相关疑难解决方法(0)

将Yaml中的列表映射到Spring Boot中的对象列表

在我的Spring Boot应用程序中,我有application.yaml配置文件,其中包含以下内容.我想将它作为配置对象注入通道配置列表:

available-payment-channels-list:
  xyz: "123"
  channelConfigurations:
    -
      name: "Company X"
      companyBankAccount: "1000200030004000"
    -
      name: "Company Y"
      companyBankAccount: "1000200030004000"
Run Code Online (Sandbox Code Playgroud)

和@Configuration对象我想填充PaymentConfiguration对象列表:

    @ConfigurationProperties(prefix = "available-payment-channels-list")
    @Configuration
    @RefreshScope
    public class AvailableChannelsConfiguration {

        private String xyz;

        private List<ChannelConfiguration> channelConfigurations;

        public AvailableChannelsConfiguration(String xyz, List<ChannelConfiguration> channelConfigurations) {
            this.xyz = xyz;
            this.channelConfigurations = channelConfigurations;
        }

        public AvailableChannelsConfiguration() {

        }

        // getters, setters


        @ConfigurationProperties(prefix = "available-payment-channels-list.channelConfigurations")
        @Configuration
        public static class ChannelConfiguration {
            private String name;
            private String companyBankAccount;

            public ChannelConfiguration(String name, String companyBankAccount) {
                this.name = name;
                this.companyBankAccount = …
Run Code Online (Sandbox Code Playgroud)

java spring yaml spring-boot

47
推荐指数
5
解决办法
8万
查看次数

将spring @value注释评估为原始布尔值

我有一个spring @configuration注释类MappingsClientConfig,其布尔字段为:

 @Value("${mappings.enabled:true}")
    private boolean mappingsEnabled;
Run Code Online (Sandbox Code Playgroud)

这个类被导入另一个spring注释类,如下所示:

@Configuration
@Import(MappingsClientConfig.class)
public class LookupManagerConfig {
Run Code Online (Sandbox Code Playgroud)

当在测试用例中通过Spring上下文实例化类时,容器无法将字符串解析为布尔字段mappingsEnabled,我得到:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private boolean com.barcap.tcw.mappings.economic.client.config.EconomicMappingsClientConfig.economicMappingsEnabled; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'boolean'; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [${mappings.enabled:true}]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
    ... 138 more
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'boolean'; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [${mappings.enabled:true}]
    at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:61) …
Run Code Online (Sandbox Code Playgroud)

spring annotations

19
推荐指数
2
解决办法
4万
查看次数

标签 统计

spring ×2

annotations ×1

java ×1

spring-boot ×1

yaml ×1