将yaml映射到SpringBoot中的对象hashmap

时间:2017-04-19 21:24:04

标签: java yaml spring-boot-configuration

我正在尝试使用我的Spring启动应用程序中的String Key和PromotionPolicy值将yml文件映射到HashMap,并使用默认的spring启动实现来解析值,但PromotionPolicy对象仅包含默认值[0,false ,当我尝试从Map中读取值时,所有实例都为false。

我的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映射到自定义对象的Map中。

4 个答案:

答案 0 :(得分:5)

将您的yml更改为

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

答案 1 :(得分:1)

  • 这对我有用

YML:

property-name: '{
  key1: "value1",
  key2: "value2"
}'

Spring Boot:

  @Value("#{${property-name}}")
  private Map<String,String> myMap;

通过https://relentlesscoding.com/2018/09/09/spring-basics-dynamically-inject-values-with-springs-value/

找到了答案

答案 2 :(得分:0)

问题出在空格,下面的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

答案 3 :(得分:0)

...
<form method="post" th:action="@{/myTable}" th:object="${requestIds}">
   <table class="table ...">
      <thead>
      <tr>
         <th><input type="checkbox" class="selectall" data-target="requests-all"/></th>
         <th>Name</th>
         <th>Phone</th>
         <th>Email</th>
      </tr>
      </thead>
      <tbody>
      <tr role="row" th:each="request : ${requests}">
         <td>
            <input type="checkbox" name="requestId" data-target="requests-all" 
                   th:value="${request.id}" th:field="*{requestIds}"/>
         </td>
         <td th:text="${request.name}"></td>
         <td th:text="${request.phone}"></td>
         <td th:text="${request.email}"></td>
      </tr>
      </tbody>
   </table>
   <button class="btn btn-primary show-selected">Process</button>
</form>
...
<script>
$(document).ready(function() {
   $('button').click(function(e) {
      if !(confirm("Are you sure you wish to process these requests")) {
         e.preventDefault();
      }
   });
});
</script>
...

Java代码:

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