Java 在SpringBoot中将yaml映射到对象hashmap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43506319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Map yaml to object hashmap in SpringBoot
提问by Manjunath
I am trying to Map the yml file to a HashMap with String Key and PromotionPolicy value in my Spring boot application and using the default spring boot implementation to parse the values, but the PromotionPolicy object only contains the default values [0, false, false] for all instances when I try to read values from the Map.
我正在尝试将 yml 文件映射到 Spring Boot 应用程序中带有 String Key 和 PromotionPolicy 值的 HashMap,并使用默认的 Spring Boot 实现来解析这些值,但 PromotionPolicy 对象仅包含默认值 [0, false, false]对于我尝试从 Map 读取值的所有情况。
My yml is :
我的 yml 是:
promotionPolicies :
policies:
P001NN:
PromotionPolicy:
expiryPeriodInDays: 16
reusable: true
resetExpiry: false
P001YN:
PromotionPolicy:
expiryPeriodInDays:1
reusable:true
resetExpiry:false
P001NY:
PromotionPolicy:
expiryPeriodInDays:1
reusable:false
resetExpiry:true
The Model I have is :
我拥有的模型是:
public class PromotionPolicy {
private int expiryPeriodInDays;
private boolean reusable;
private boolean resetExpiry;
public int getExpiryPeriodInDays() {
return expiryPeriodInDays;
}
public void setExpiryPeriodInDays(int expiryPeriodInDays) {
this.expiryPeriodInDays = expiryPeriodInDays;
}
public boolean isReusable() {
return reusable;
}
public void setReusable(boolean reusable) {
this.reusable = reusable;
}
public boolean isResetExpiry() {
return resetExpiry;
}
public void setResetExpiry(boolean resetExpiry) {
this.resetExpiry = resetExpiry;
}
}
The component java class is as below:
组件java类如下:
@Configuration
@ConfigurationProperties(prefix = "promotionPolicies")
@EnableConfigurationProperties
@Component
public class PromotionPolicyConfig {
private Map<String, PromotionPolicy> policies = new HashMap<String, PromotionPolicy>();
public void setPolicies(Map<String, PromotionPolicy> policies) {
this.policies = policies;
}
public Map<String, PromotionPolicy> getPolicies() {
return policies;
}
}
Trying to display values here :
尝试在此处显示值:
@RestController
@RequestMapping("/test")
public class LoyaltyServiceController {
@Autowired
PromotionPolicyConfig promotionPolicyConfig;
@RequestMapping(value = "/try")
public String tryThis() {
for (Entry<String, PromotionPolicy> entry : promotionPolicyConfig.getPolicies().entrySet()) {
System.out.print(entry.getKey() + " : ");
System.out.print(entry.getValue() + " : ");
System.out.print(entry.getValue().getExpiryPeriodInDays() + " : ");
System.out.print(entry.getValue().isResetExpiry() + " : ");
System.out.println(entry.getValue().isReusable() + " : ");
}
}
My Output is as below:
我的输出如下:
P001NN : com.expedia.www.host.loyalty.model.PromotionPolicy@63a1c99b : 0 : false : false :
P001YN : com.expedia.www.host.loyalty.model.PromotionPolicy@7892b6b6 : 0 : false : false :
P001NY : com.expedia.www.host.loyalty.model.PromotionPolicy@459928ab : 0 : false : false :
while I expected the result to contain the values in my yml. I also tried removing the line "PromotionPolicy:" in my yml, but no luck.
虽然我希望结果包含我的 yml 中的值。我还尝试在我的 yml 中删除“PromotionPolicy:”行,但没有成功。
Request help understand how can I Map the yml into the Map of custom objects.
请求帮助了解如何将 yml 映射到自定义对象的映射中。
回答by pvpkiran
change your yml to
将您的 yml 更改为
promotionPolicies :
policies:
P001NN:
expiryPeriodInDays: 16
reusable: true
resetExpiry: false
P001YN:
expiryPeriodInDays: 1
reusable: true
resetExpiry: false
P001NY:
expiryPeriodInDays: 1
reusable: false
resetExpiry: true
回答by Manjunath
The problem was with the spaces, the below yml works fine. The spaces after the text and : was required
问题出在空格上,下面的 yml 工作正常。文本和 : 后的空格是必需的
promotionPolicies :
policies :
P001NN :
PromotionPolicy :
expiryPeriodInDays: 16
reusable: true
resetExpiry: false
P001YN :
PromotionPolicy :
expiryPeriodInDays:1
reusable:true
resetExpiry:false
P001NY :
PromotionPolicy :
expiryPeriodInDays:1
reusable:false
resetExpiry:true
回答by csf
- This worked for me
- 这对我有用
YML:
YML:
property-name: '{
key1: "value1",
key2: "value2"
}'
Spring Boot:
弹簧靴:
@Value("#{${property-name}}")
private Map<String,String> myMap;
found the answer thanks to : https://relentlesscoding.com/2018/09/09/spring-basics-dynamically-inject-values-with-springs-value/
感谢找到答案:https: //relentlesscoding.com/2018/09/09/spring-basics-dynamically-inject-values-with-springs-value/