2

The current implementation is :-

public static final List<Integer> validCodes = Collections.unmodifiableList(Arrays.asList(110, 210, 310,510,610));

However, I am not happy with this approach as this of hard coded. I want to make it configurable instead of hard-coded. I suppose reading this values from the yaml file would solve my problem.

But how do I define a list of Integer in the yaml file and real it using @value. I can find a lot of example about reading list of strings but not integers.

Rishab Prasad
  • 771
  • 1
  • 8
  • 21
  • This too can do `int[] arr = (int[]) Yaml.load(new File("file.yml"));` – Vebbie Mar 08 '19 at 10:46
  • I don't want to read it from a file as well. I want to make it as a configurable property in the yaml file @Vebbie – Rishab Prasad Mar 08 '19 at 10:48
  • 2
    Possible duplicate of [Get Integer\[\] instead of ArrayList from YAML file](https://stackoverflow.com/questions/41685167/get-integer-instead-of-arraylistinteger-from-yaml-file) – arnt Mar 08 '19 at 12:17

3 Answers3

1

Within the application.yml file, define the config variable as follows.

my_integers: 1231,12323,12321

then use the @Value annotation to make to an Integer list as follows.

@Value("#{'${my_integers}'.split(',')}")
private List<Integer> myIntegers;

Spring does the rest. You only need to use myIntegers with your need. Happy coding....

PS: I have used spring boot 2.0.*

0

One way:

Let's say if the properties file contains

intArr={1,2,3}

Then @Value can be used like:

@Value("#{${intArr}}")
Integer[] intArr;

Second way:

If the property contains comma separated values as: intArr: [1, 2, 3]

Then the annotation code would be:

@Value("${intArr}")
private int[] intArr;

Edit:

You can configure ConversionServiceFactoryBean which activates the new configuration service which supports converting String to Collection types.

By activating this, it will support following kind of conversion:

 intArray= 1, 2, 3, 4

And the following code:

@Value("${intArray}")
private List<Integer> myList;

Ref here

Vebbie
  • 1,669
  • 2
  • 12
  • 18
  • I tried the second way:- intArr: [1, 2, 3] @Value("${intArr}") private List intArr; Not working!! – Rishab Prasad Mar 08 '19 at 11:39
  • Have you declared any prefix to append before `intArr` in yaml properties ? – Vebbie Mar 08 '19 at 11:46
  • Nope, no prefix. It's giving a null value @Vebbie – Rishab Prasad Mar 08 '19 at 12:14
  • Okay. Editing answer with one more approach. – Vebbie Mar 08 '19 at 12:15
  • Sorry, I was using a static variable. It was the reason for null value. However, the approach doesn't work even for a non-static variable. It says cannot resolve the placeholder. however, If i give it as intArr: '1,2,3' , its working. I guess '[]' bracket is creating issues. However if I use intArr: '1,2,3' , It becomes a list of String, but I want list of Integer – Rishab Prasad Mar 08 '19 at 12:38
  • In that case you can convert `List` to `List` like this: `for(String s : strList) intList.add(Integer.valueOf(s));` – Vebbie Mar 08 '19 at 13:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189680/discussion-between-vebbie-and-boy). – Vebbie Mar 08 '19 at 13:27
0

You can used following method for getting all available properties from YML file. When you used following method, you have to add following jar into your build script.

compile group: 'org.yaml', name: 'snakeyaml', version: '1.12'   

I think this will help you to continue your task.

private Map<String, Object> loadYMLData() {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String fileName = "{{YAMAL FILE NAME}}.yml";
            Yaml yaml = new Yaml();
            ClassLoader classLoader = getClass().getClassLoader();
            File file = new File(classLoader.getResource(fileName).getFile());
            InputStream ios = new FileInputStream(file);

            // Parse the YAML file and return the output as a series of Maps and Lists
            result = (Map<String, Object>) yaml.load(ios);
            System.out.println(result.toString());

        } catch (Exception e) {
            logger.error("Error==>", e);
        }
        return result;
    }
Thusitha Indunil
  • 832
  • 1
  • 8
  • 22