0

I have the following code:

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

But I get the following exception:

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 {

}

and my applicatin-test.properties:

optional.currencies={}

What am I doing wrong?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
sewey
  • 51
  • 3
  • 9

2 Answers2

0

You need to set optional.currencies to be a map of sets, like

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

Can you please let me know if this solves your issue?

Kaj Hejer
  • 955
  • 4
  • 18
  • Hi, what if I want it to be empty ? This is a param that will be needed only in test environment. But in Live environment, this property must not be set; – sewey Dec 18 '20 at 15:03
  • It might work to just not set it in live environment and set it empty as defaultvalue using a ```:```, see https://www.baeldung.com/spring-value-defaults. In this case I think you can just leave it empty after the ```:```. – Kaj Hejer Dec 18 '20 at 15:18
0

Try this -

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

Hema
  • 23
  • 4