简体   繁体   中英

Read config from application.yml to Map of List of POJO

I have this application.yml file

messages:
  first-journey-messages:
    - key: fullName-required
      value: This field is required.
    - key: fullName-validation
      value: Please enter a valid full name.
    - key: technical-error
      value: Unfortunately we are having technical issues, please try again later.
  second-journey-messages:
    - key: technical-error
      value: "<p class='heading-bold'>Sorry, something went wrong </p><p>We encountered a technical error. Please try again. If you continue to experience difficulties, please call us on 0123456 and an agent will be happy to assist you.</p>"
    - key: download-technical-error
      value: "<p class='heading-bold'>Sorry, something went wrong </p><p>Unable to download document at this moment. Please try again later.</p>"
  third-journey-messages:
    - key: home-url
      value: /app/home
    - key: services-url
      value: /app/services

I want to read above configuration in below Map object using @Value annotation.

@Value("${messages}")
private Map<String, List<MessageEntry>> messagesMap = new HashMap<>();

POJO: MessageEntry

@Data
@AllArgsConstructor
public class MessageEntry implements Serializable {
    private String key;
    private String value;
}

When I am trying to read the config, I am getting null .

messagesMap.get("second-journey-messages")

Can someone help me to identify, what am I doing incorrectly? Or, how can I read the above config as object of type Map<String, List<MessageEntry>> ?

Note: I am using spring boot with lombok

I think this answear could help You link . Try to change key, value notation in your yml file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM