java – How to make an if comparing a value with a list in a yml?

Question:

I get a request and I want to check if that channel is in a list I created in yml.

My method:

    private void insereNome (String canal, ListaNomeWebRequest listaNomeWebRequest){
    if(canal.contains(("${canais}"))){
        listaNomeWebRequest.setStatus("OK");
    }
}

where:

canais:
  abc: abc

This way, he is not able to check if it contains the abc channel to compare. can you help me?

Note: I've tried it without the $.

Answer:

I suggest you inject the values ​​from your file using @ConfigurationProperties :

@ConfigurationProperties(prefix="app")
public class MinhaClasse {
    private List<String> canais = new ArrayList<String>();

    public void insereNome (String canal, ListaNomeWebRequest listaNomeWebRequest){
        if( canais.contains(canal) ) {
            listaNomeWebRequest.setStatus("OK");
        }
    }

    public List<String> getCanais() {
        return this.canais;
    }
}

Your yaml file is also in the wrong structure, you should use a list as below:

app:
    canais:
         - abc
         - bcd
         - cde
         - xyz

Spring Boot Documentation


Edit

Upgrading to a working example

ConfigurationPropertiesDemoApplication.java

package com.example.demo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class ConfigurationPropertiesDemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctxt = SpringApplication.run(ConfigurationPropertiesDemoApplication.class, args);

        ctxt.getBean(ConfigurationPropertiesExample.class).printChannels();
    }

    @Component
    @ConfigurationProperties(prefix = "app")
    public class ConfigurationPropertiesExample {

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

        public void printChannels() {
            System.out.println(channels);
        }

        public List<String> getChannels() {
            return channels;
        }

    }
}

application.yaml

app:
  channels:
    - abc
    - cdb
    - xyz

The output when uploading the project is the channel list configured in application.yaml :

 :: Spring Boot ::        (v2.1.5.RELEASE)

2019-06-01 18:36:44.618  INFO 1252 --- [  restartedMain] d.ConfigurationPropertiesDemoApplication : Starting ConfigurationPropertiesDemoApplication on DESKTOP-EBSB0DB with PID 1252 (C:\projects\configuration-properties-demo\target\classes started by Norton Gueno in C:\projects\configuration-properties-demo)
2019-06-01 18:36:44.620  INFO 1252 --- [  restartedMain] d.ConfigurationPropertiesDemoApplication : No active profile set, falling back to default profiles: default
2019-06-01 18:36:44.647  INFO 1252 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-06-01 18:36:45.041  INFO 1252 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-06-01 18:36:45.057  INFO 1252 --- [  restartedMain] d.ConfigurationPropertiesDemoApplication : Started ConfigurationPropertiesDemoApplication in 0.724 seconds (JVM running for 1.366)
[abc, cdb, xyz]
Scroll to Top