Map yaml to object hashmap in SpringBoot

Manjunath picture Manjunath · Apr 19, 2017 · Viewed 36.9k times · Source

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.

My yml is :

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:

@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.

Request help understand how can I Map the yml into the Map of custom objects.

Answer

pvpkiran picture pvpkiran · Apr 20, 2017

change your yml to

promotionPolicies : 
  policies: 
    P001NN:
            expiryPeriodInDays: 16
            reusable: true
            resetExpiry: false
    P001YN:
            expiryPeriodInDays: 1
            reusable: true
            resetExpiry: false
    P001NY:
            expiryPeriodInDays: 1
            reusable: false
            resetExpiry: true