0

I have the following config and java code to map the yml config however the rest, soap and javascript properties are not getting mapped.

Tried the approach given here in accepted answer but no luck https://stackoverflow.com/a/50410542/10644550

Spring boot version: spring-boot-2.7.0

Any help would be appreciated.

application.yml

bpmn:
  prop:
    rest:
    - http-rest-service
    - REST
    soap:
      - http-soap-service
      - SOAP
    javascript:
      - JAVASCRIPT
      - JS-script-worker
    test1: 1235

Java code:

package com.example.orchestration.properties;

import lombok.Data;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Set;

@Configuration
@Component
@ConfigurationProperties(prefix = "bpmn.prop")
@PropertySource("application.yml")
@Setter
public class BpmnProperties {


   //@Value("${rest}")
   Set<String> rest;
    //@Value("soap")
    Set<String> soap;
   // @Value("javascript")
    Set<String> javascript;

    String test1;




    public boolean isRest(String str){
        return rest.contains(str);
    }
    public boolean isSoap(String soap){
        return this.soap.contains(soap);
    }
    public boolean isJs(String js){
        return javascript.contains(js);
    }

    @PostConstruct
    public void init(){
        System.out.println(rest);
        System.out.println("test "+test1);
    }

}
Shiva
  • 1,962
  • 2
  • 13
  • 31
  • _"the rest, soap and javascript properties are not getting mapped correctly"_ what is happening vs. what you're expecting? – Kaan Jul 02 '22 at 17:16
  • they are `null` .. to be more precise, values are not mapped – Shiva Jul 02 '22 at 17:18
  • Is this your entire code? If so, you didn't initiate your sets! When you create a Object field (like your sets) in Java the default value is null. Try `Set mySet = new HashSet<>();`. But even then they are empty and don't have any content in it, so you would have to fill them with the content from your YAML file. (use an API) – Japhei Jul 02 '22 at 17:28
  • In addition you can't just print out a Set or any Object without a `toString()` method. You have to iterate through it element by element. – Japhei Jul 02 '22 at 17:30
  • hey @Japhei, please try before commenting.. – Shiva Jul 02 '22 at 17:34
  • Do you have setters for rest and soap properties? – Dirk Deyne Jul 02 '22 at 17:47
  • @DirkDeyne no setters – Shiva Jul 02 '22 at 17:50
  • you need setters for ConfigurationProperties to work – Dirk Deyne Jul 02 '22 at 17:51
  • @DirkDeyne Tried that too however no luck – Shiva Jul 02 '22 at 17:56
  • @DirkDeyne edited question with setter.. please see `@Setter` annotation and in fact nothing is getting set for example, `test1` above. – Shiva Jul 02 '22 at 17:59
  • location application.yml correct? test this via @PropertySource(value = "application.yml", ignoreResourceNotFound = false). Lombok correctly configured? test via using a normal setter. – Dirk Deyne Jul 02 '22 at 18:27
  • [working demo](https://github.com/dirkdeyne/sso-yml-config) – Dirk Deyne Jul 02 '22 at 18:37

1 Answers1

0

Since Spring Boot 2.2 you can simplify your annotations to

@ConfigurationProperties(prefix = "bpmn.prop")
public class BpmnProperties {

and add

@ConfigurationPropertiesScan("com.example.orchestration.properties")

to your main class.

curiousdev
  • 626
  • 8
  • 24