0

application.yml file

countries:['India','USA']

How to read this countries parameters in Java with @Value annotation?

Stultuske
  • 9,296
  • 1
  • 25
  • 37
Logesh S
  • 63
  • 4
  • Refer: https://stackoverflow.com/questions/26699385/spring-boot-yaml-configuration-for-a-list-of-strings/26700938 – Ashish Karn Feb 27 '20 at 11:07
  • Does this answer your question? [Spring Boot yaml configuration for a list of strings](https://stackoverflow.com/questions/26699385/spring-boot-yaml-configuration-for-a-list-of-strings) – Djib2011 Feb 27 '20 at 12:23

2 Answers2

0

use comma separated values in application.yml

countries: India, USA

java code for access

@Value("${countries}")
String[] countries

Yogendra Mishra
  • 2,399
  • 2
  • 13
  • 20
0

First Approach:

Your YAML list should look like this:

acme:
  countries:
    - India
    - USA

Then add the following annotation to your Country class:

@ConfigurationProperties(prefix = "acme")
public class Country {

    private List<String> countries = new ArrayList<>();

   //constructor, getter/setter etc.

}

Lastly you need to add another annotation to your SpringBootApplication app:

@SpringBootApplication
@EnableConfigurationProperties(Country.class)

Finally get the countries by autowiring the Country class and calling the getter-method.

Second Approach:

YAML:

countries: India, USA

Country.class

@Value("${countries}")
private List<String> countries;
Samuel
  • 1
  • 1