java - 从 application.yml 读取配置到 POJO 列表的 Map
问题描述
我有这个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
我想使用注释在下面的Map
对象中阅读上面的配置。@Value
@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<String, List<MessageEntry>>
?
注意:我正在使用带有lombok的弹簧靴
解决方案
我认为这个答案可以帮助你链接。尝试更改 yml 文件中的键值符号。
推荐阅读
- java - Soap 服务无法写入 XML 文件
- ios - 2 个带有前导对齐和不同字体大小的标签
- python - Django 会话而不是用户模型
- javascript - 如何检查“Enter”键的输入
- blazor - Blazor,可选表行内的可选复选框
- reactjs - 我使用 expo CLI 在 reactjs 中创建了我的第一个应用程序,并在尝试启动它时出现此错误
- python-3.x - 无法使用 pulumi 在 aws 上通过事件桥触发 lambda
- amazon-web-services - “aws cloudformation delete-stack”不能使用 SAM CLI 使用的 StackName?
- c - 使用 realloc 的问题(旧大小)
- sql - 如何在 sequelize 中使用 group by 与当前年份月份和另一个数据字段