How to make an if comparing values with a list that is in an yml?

Asked

Viewed 59 times

1

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

@ConfigurationProperties(prefix = "parceiros")
@Component
public class CanaisMapper {

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

private void verificaCanal(String canal, Status status){
    if(canais.contains(canal)) {
        status.setStatus("OK");
    }
  }
}

my yml:

parceiros:
  canais:
    - abc

I’m taking Nullpointer. If in if I put if(channel == "abc"), it works. But I want to take from this yml list to insert more channels with this condition.

Could you help me?

1 answer

0


Missing include a getter for channel list:

@ConfigurationProperties(prefix = "parceiros")
@Component
public class CanaisMapper {

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

    private void verificaCanal(String canal, Status status){
        if(canais.contains(canal)) {
            status.setStatus("OK");
        }
    }

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

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]
  • I added the getter, it’s exactly like this and to taking nullpointter. Using the Debugger, channels are channels: size = 0 :(

  • @Mssantana includes a functional example

Browser other questions tagged

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