我想将我的Spring引导应用程序中的yml文件映射到带有String Key和PromotionPolicy值的HashMap,并使用默认的spring引导实现来解析值,但PromotionPolicy对象只包含当我尝试从Map读取值时,所有实例的默认值[0,false,false]。将yaml映射到SpringBoot中的对象hashmap
我阳明是:
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
我的型号是:
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;
}
}
组件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;
}
}
想在这里显示的值:
@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() + " : ");
}
}
我的输出是如下:
P001NN : [email protected] : 0 : false : false :
P001YN : [email protected] : 0 : false : false :
P001NY : [email protected] : 0 : false : false :
,而我希望的结果包含在我的阳明的值。 我也试过在我的yml中删除行“PromotionPolicy:”,但没有运气。
请求帮助了解如何将yml映射到自定义对象的地图。
可能是这将有助于 http://stackoverflow.com/questions/32593014/mapping-list-in-yaml-to-list-of-objects-in-春季启动 – pvpkiran