![](/img/trans.png)
[英]How do I read config values from application.yml in Spring 2.7.2
[英]Read config from application.yml to Map of List of POJO
我有这个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<String, List<MessageEntry>>
类型的对象?
注意:我正在将Spring Boot与Lombok一起使用
我认为这可以帮助您链接 。 尝试更改yml文件中的键,值表示法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.