Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map yaml to object hashmap in SpringBoot

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.

like image 963
Manjunath Avatar asked Apr 19 '17 21:04

Manjunath


People also ask

How do I map in YAML?

In YAML, maps are represented using the colon ( : ) character. We can also use quotation marks ( " or ' ) to enclose the keys and values if we need to.

How do you send a map in Yml app?

How to Inject a Map From a YAML File. Spring Boot has taken data externalization to the next level by providing a handy annotation called @ConfigurationProperties. This annotation is introduced to easily inject external properties from configuration files directly into Java objects.


1 Answers

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
like image 158
pvpkiran Avatar answered Sep 23 '22 08:09

pvpkiran