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

Asked

Viewed 145 times

3

I get a request and 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");
    }
}

my yml:

canais:
  abc: abc

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

Note: I have tried without the $.

1 answer

2


I suggest you inject the values of 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

Documentation of Spring Boot


Edit

Upgrading to a functional 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

Output when uploading the project is from 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]
  • The night I make an example and put on Github ;)

  • @Mssantana added a getter that missed the answer, try now

Browser other questions tagged

You are not signed in. Login or sign up in order to post.