java - 在 SpringBoot 中将 yaml 映射到对象 hashmap

标签 java yaml spring-boot-configuration

我试图在我的 Spring Boot 应用程序中将 yml 文件映射到具有字符串键和 PromotionPolicy 值的 HashMap,并使用默认的 Spring Boot 实现来解析值,但 PromotionPolicy 对象仅包含默认值 [0, false , false] 对于我尝试从 Map 读取值的所有实例。

我的 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

我的模型是:

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 : 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 : 

虽然我希望结果包含我的 yml 中的值。 我还尝试删除我的 yml 中的“PromotionPolicy:”行,但没有成功。

请求帮助了解如何将 yml 映射到自定义对象的映射中。

最佳答案

把你的yml改成

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

关于java - 在 SpringBoot 中将 yaml 映射到对象 hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43506319/

相关文章:

java - 当我更改单独的代码时,JButton 有时会起作用,而其他则不起作用

java - 匹配正则表达式模式时如何忽略变音符号?

mysql - 在 Rails 中使用变异元音作为数据库密码

spring-boot - Git 上的 Spring Boot 配置

kotlin - 如何将 SpringBootTest 的本地端口转发到测试配置

java - 使用 Winium 实现 "Chrome Legacy Window"(Chromium) 的自动化

java - 关于 javax.persistence JAR 的 Maven 依赖项?

amazon-web-services - CloudFormation AWS::EC2::路由

amazon-web-services - 如何在yml中正确使用CloudFormation的内部函数

spring-boot - 如何禁用 TomcatServletWebServerFactory 的 SpringBoot 自动配置以便自定义 spring-starter 提供它?