【问题标题】:Read config from application.yml to Map of List of POJO从 application.yml 读取配置到 POJO 列表的 Map
【发布时间】:2019-11-22 10:25:51
【问题描述】:

我有这个 application.yml 文件

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

我想使用@Value 注释在Map 对象下方读取上述配置。

@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;
}

当我尝试读取配置时,我收到了null

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

有人可以帮我识别,我做错了什么吗? 或者,我如何将上述配置读取为Map&lt;String, List&lt;MessageEntry&gt;&gt; 类型的对象?

注意:我使用 spring bootlombok

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    我认为这个答案可以帮助你link。尝试更改 yml 文件中的键、值表示法。

    【讨论】:

      猜你喜欢
      • 2022-11-14
      • 2019-04-06
      • 2020-05-18
      • 2022-10-04
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多