1

I am trying to inject a list of maps from my application.yml configuration file into my Spring Boot service. Here is the configuration in application.yml:

devoxx:
  cfpApis:
    -
      url: http://cfp.devoxx.be/api/conferences
      youtubeChannelId: UCCBVCTuk6uJrN3iFV_3vurg
    -
      url: http://cfp.devoxx.fr/api/conferences
    -
      url: http://cfp.devoxx.ma/api/conferences
      youtubeChannelId: UC6vfGtsJr5RoBQBcHg24XQw
    -
      url: http://cfp.devoxx.co.uk/api/conferences
    -
      url: http://cfp.devoxx.pl/api/conferences

And here is my property in my service:

@Value("devoxx.cfpApis")
List<Map<String,String>> cfpApis

But there must be something wrong because when I try to run my application, I get the following exception:

java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Map]: no matching editors or conversion strategy found

Any idea of what I'm doing wrong?

FYI, I'm trying to migrate a Grails 3 project into a vanilla Spring Boot project and this configuration works in Grails 3, but Grails has its own YAML processors.

Sebastien
  • 3,583
  • 4
  • 43
  • 82
  • 1
    I haven't used `yaml` files so far, but you typically reference a property placeholder using `$` like `@Value("${devoxx.cfpApis}")`. Nonetheless, the [spring documentation](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-loading-yaml) seems to suggest a somewhat different approach using `@ConfigurationProperties`. Perhaps [this other question](http://stackoverflow.com/questions/24917194/spring-boot-inject-map-from-application-yml) can be of some help – Morfic Jan 14 '16 at 17:02
  • This worked great. Thanks. I will add an answer detailing this – Sebastien Jan 15 '16 at 16:06
  • Cheers, glad I could help :) – Morfic Jan 15 '16 at 16:13

1 Answers1

4

Thanks to @Morfic's comment, here is how i ended up solving that problem.

I tagged my service class with @ConfigurationProperties(prefix="devoxx") annotation. And in my service, I now have a property called cfpApis, with the following declaration:

List<Map<String,String>> cfpApis

And this works great.

Sebastien
  • 3,583
  • 4
  • 43
  • 82