3

I have this values in application.yaml

import-charge-fields:
  4: billDate
  1001: validUntil
  7: totalAmount
  24: purpose

And in service I try inject it:

    @Value("${import-charge-fields}")
    private Map<String, String> fields;

But I get exception:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'import-charge-fields' in value "${import-charge-fields}"

I try this:

charge:
  hz: 123
  fields:
    4: billDate
    1001: validUntil
    7: totalAmount
    24: purpose

and

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "charge")
public class ChargeFields {
   private String hz;
   private Map<String, String> fields;
}

and

@Value("${charge.hz}")
private String hz;

@Value("${charge.fields}")
private Map<String, String> fields;

and it not work

ip696
  • 6,574
  • 12
  • 65
  • 128
  • 1
    What about: https://stackoverflow.com/questions/24917194/spring-boot-inject-map-from-application-yml ? – Pijotrek Feb 08 '19 at 09:31
  • It's exactly as mentioned in the other question, use `@ConfigurationProperties` for complex structures. Considering that this is a duplicate, I voted to close this question. – g00glen00b Feb 08 '19 at 09:37
  • Have you tried with `@Resource` – Shiva Feb 08 '19 at 10:11
  • Possible duplicate of [Spring Boot - inject map from application.yml](https://stackoverflow.com/questions/24917194/spring-boot-inject-map-from-application-yml) – Patrick Feb 08 '19 at 13:55

2 Answers2

0

I was getting the same problem and after a lot of attempts my problem was in the Set Method.

Using @EnableConfigurationProperties You must to implement setFields() method.

Varjão
  • 31
  • 3
0

Numbers aren't valid fieldnames in yaml. Try adding quotes:

import-charge-fields:
  "4": billDate
  "1001": validUntil
  "7": totalAmount
  "24": purpose

All your java attempts are valid and should be working.

George
  • 2,820
  • 4
  • 29
  • 56