【问题标题】:@Value with map of sets [duplicate]@Value 与集合图 [重复]
【发布时间】:2021-03-29 04:44:09
【问题描述】:

我有以下代码:

@Value("#{${optional.currencies}}") 
private Map<Account, Set<String>> mapAccountAndCurrencies= null;

但我得到以下异常:

Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.util.Collections$UnmodifiableRandomAccessList' to required type 'java.util.Map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.util.Collections$UnmodifiableRandomAccessList' to required type 'java.util.Map': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:76)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1235)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:636)
    ... 68 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.util.Collections$UnmodifiableRandomAccessList' to required type 'java.util.Map': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:262)
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:73)
    ... 71 more
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBatchTest
@EnableAutoConfiguration
@ContextConfiguration(classes= {BatchTestConfiguration.class})
@TestPropertySource(locations="classpath:application-test.properties")
public class BatchTest {

}

还有我的 applicatin-test.properties:

optional.currencies={}

我做错了什么?

【问题讨论】:

  • 您好,请问您如何设置属性optional.currencies?谢谢!
  • 感谢@KajHejer。我已经编辑了我的帖子
  • 这对我有用,没有任何问题@sewey
  • @NicoVanBelle 仍然有同样的问题。我的实际批处理运行良好(使用类似的 application.properties)。只有在运行 BatchTest 时才会出现此错误。

标签: java spring-boot properties


【解决方案1】:

试试这个 -

@Value("#{${optional.currencies:{null:null}}}") 
private Map<Account, Set<String>> mapAccountAndCurrencies= null;

【讨论】:

    【解决方案2】:

    您需要将optional.currencies 设置为集合的映射,例如

    optional.currencies={'account1': {'USD','GBP'}, 'account2': {'USD','NOK'}}
    

    如果这能解决您的问题,请告诉我?

    【讨论】:

    • 嗨,如果我想让它为空怎么办?这是一个仅在测试环境中需要的参数。但在 Live 环境中,这个属性不能设置;
    • 在实时环境中不设置它并使用:将其设置为默认值可能会起作用,请参阅baeldung.com/spring-value-defaults。在这种情况下,我认为您可以在 : 之后将其留空。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多