0
votes

Hello Team,

I recently tried reading contents from application.yml file in a Spring Boot project (Version 2.3.4).

Initially, all the properties from yml file were getting read as null.

After cleaning and rebuilding project several times, I could read all the properties except the List of user defined class object (List<LogComponents> in below class) which is still getting read as null.

I tried all the possible solutions but nothing worked for me.

Could you please check and help me in understanding what I have missed in below code because of which the value for List<LogComponent> logComponents is still getting read as null from yml file?

Thanking you in anticipation!

Configuration Java Class


    @Configuration
    @EnableConfigurationProperties
    @ConfigurationProperties
    public class TestAPIConfiguration {
     
        private String eventCache;
        
        private String diskBasedCache;
    
        private List<String> sendAllSMSto;
    
        private List<String> sendAllEmailsto;
        
        //This property is getting read as null even if
        //value for this property is present in yml file.
        private List<LogComponent> logComponents;
        
                
        @NotNull
        private String selfURIPrefix;
        
        @NotNull
        private String investURIPrefix;
        
        @NotNull
        private String ifaURIPrefix;
        
        private String apiEnv;
        
        private final Joiner joiner = Joiner.on( "," ).skipNulls();
        
        private static final Logger LOGGER = LoggerFactory.getLogger(TestAPIConfiguration.class);
        
        @PostConstruct
        public void setSystemProperties()
        {
            try
            {
                System.setProperty(SystemConstants.EVENT_CACHE_PATH, eventCache);
                System.setProperty(SystemConstants.DISK_BASED_CACHE_PATH, diskBasedCache);
                System.setProperty(SystemConstants.REQUEST_LOGGING_FIELDS, 
                        JSONUtils.getObjectMapper().writeValueAsString(logComponents));
                System.setProperty(SystemConstants.ENVIRONMENT_IDENTIFIER, apiEnv);
                System.setProperty(INVEST_URI_PREFIX, investURIPrefix);
                System.setProperty(IFA_URI_PREFIX, ifaURIPrefix);
                if(sendAllSMSto != null)
                    System.setProperty(SEND_ALL_SMS_TO, joiner.join(sendAllSMSto));
                if(sendAllEmailsto != null)
                    System.setProperty(SystemConstants.SEND_ALL_EMAILS_TO, joiner.join(sendAllEmailsto));
            }
            catch(Exception se)
            {
                LOGGER.error("Error in Configuration Setup: {}", se.getLocalizedMessage());
            }
        }
    
        public String getEventCache() {
            return eventCache;
        }
    
        public void setEventCache(String eventCache) {
            this.eventCache = eventCache;
        }
    
        public String getDiskBasedCache() {
            return diskBasedCache;
        }
    
        public void setDiskBasedCache(String diskBasedCache) {
            this.diskBasedCache = diskBasedCache;
        }
    
        public List getSendAllSMSto() {
            return sendAllSMSto;
        }
    
        public void setSendAllSMSto(List<String> sendAllSMSto) {
            this.sendAllSMSto = sendAllSMSto;
        }
    
        public List getSendAllEmailsto() {
            return sendAllEmailsto;
        }
    
        public void setSendAllEmailsto(List<String> sendAllEmailsto) {
            this.sendAllEmailsto = sendAllEmailsto;
        }
    
        public List getRequestLoggingFields() {
            return logComponents;
        }
    
        public void setRequestLoggingFields(List<LogComponent> requestLoggingFields) {
            this.logComponents = requestLoggingFields;
        }
    
        public String getSelfURIPrefix() {
            return selfURIPrefix;
        }
    
        public void setSelfURIPrefix(String selfURIPrefix) {
            this.selfURIPrefix = selfURIPrefix;
        }
    
        public String getInvestURIPrefix() {
            return investURIPrefix;
        }
    
        public void setInvestURIPrefix(String investURIPrefix) {
            this.investURIPrefix = investURIPrefix;
        }
    
        public String getIfaURIPrefix() {
            return ifaURIPrefix;
        }
    
        public void setIfaURIPrefix(String ifaURIPrefix) {
            this.ifaURIPrefix = ifaURIPrefix;
        }
    
        public String getApiEnv() {
            return apiEnv;
        }
    
        public void setApiEnv(String apiEnv) {
            this.apiEnv = apiEnv;
        }
    }

LogComponent Java Class


    @Component
    public class LogComponent {
    
        @NotNull
        private  String headerName;
        @NotNull
        private  String sessionKey;
        @NotNull
        private  String logPrintKey;
        
        public String getHeaderName() {
            return headerName;
        }
        public String getSessionKey() {
            return sessionKey;
        }
        public String getLogPrintKey() {
            return logPrintKey;
        }   
    }

application.yml File


    debug: true
    server:
       port: 8080
    apiEnv: UAT
    selfURIPrefix: "https://testurl.localhost.net"
    investURIPrefix: "https://testurl.mediaserver.net"
    ifaURIPrefix: "https://testurl.secondaryserver.net"
    sendAllSMSto:
       - "0000000000"
    sendAllEmailsto: 
       - "[email protected]"
    eventCache: "C:\\Users\\username\\project\\devnull\\eventcachepurchase.mdb"
    diskBasedCache: "C:\\Users\\username\\project\\devnull\\cache.mdb"
    logComponents:
       - headerName: X-RT-REQUEST-TRACKER
         sessionKey: NOT AVAILABLE
         logPrintKey: REQUEST-TRACKER
       - headerName: X-RT-INX-DWD
         sessionKey: IFX-PDR
         logPrintKey: PDR_NO
       - headerName: X-RT-IFA-ARN
         sessionKey: IRX-AXRN
         logPrintKey: AXR-CDODEEE

1

1 Answers

1
votes

Finally, I found the solution.

I had not created setter methods inside the LogComponent class because of which the values were not getting assigned to the variables.

After adding the setters for all the fields, this issue has been resolved.