Spring启动YAML对象映射

时间:2016-10-08 02:10:57

标签: java spring spring-boot yaml

我正在尝试将我的Spring引导中的YAML映射到Java类,这是我的YAML和Java类

--- 
tables: 

  trs_fact_txn_detail: 
    tablespecificparams: 
        expiredinterval: "90d"
        fields: "abc"

  trs_fact_txn_detail_et: 
    tablespecificparams: 
        expiredinterval: "56d"
        fields: "abcdef"



@Configuration
@ConfigurationProperties(locations = {"${table.config.location}"})
public class RedshiftTableConfig {

    private Map<String, tablespecificparams> tables;

    public Map<String, tablespecificparams> getTables() {
        return tables;
    }

    public void setTables(Map<String, tablespecificparams> tables) {
        this.tables = tables;
    }

    public static final class tablespecificparams {

        String expiredinterval;
        String fields;

        public String getExpiredinterval() {
            return expiredinterval;
        }
        public void setExpiredinterval(String expiredinterval) {
            this.expiredinterval = expiredinterval;
        }
        public String getFields() {
            return fields;
        }
        public void setFields(String fields) {
            this.fields = fields;
        }

    }
}

我正在映射两个表。无论如何,tablespecificparameters的值似乎为null。我已经调试了一段时间。如何在此处映射tablespecificparams ?

2 个答案:

答案 0 :(得分:0)

您的配置文件应如下所示。

@Configuration
@ConfigurationProperties(locations = {"${table.config.location}",prefix = "tables"})
public class RedshiftTableConfig {

private Map<String, tablespecificparams> trs_fact_txn_detail;

public Map<String, tablespecificparams> getTrs_fact_txn_detail() {
    return trs_fact_txn_detail;
}

public void setTrs_fact_txn_detail(Map<String, tablespecificparams> trs_fact_txn_detail) {
    this.trs_fact_txn_detail = trs_fact_txn_detail;
}

public static final class tablespecificparams {

    String expiredinterval;
    String fields;

    public String getExpiredinterval() {
        return expiredinterval;
    }
    public void setExpiredinterval(String expiredinterval) {
        this.expiredinterval = expiredinterval;
    }
    public String getFields() {
        return fields;
    }
    public void setFields(String fields) {
        this.fields = fields;
     }
   }
}

yaml文件应该有适当的间隔条目。

tables: 

 trs_fact_txn_detail: 
    tablespecificparams: 
         expiredinterval: "90d"
         fields: "abc"

 trs_fact_txn_detail_et: 
        tablespecificparams: 
        expiredinterval: "56d"
        fields: "abcdef"

答案 1 :(得分:0)

我通过删除字符串&#34; bankspecificparams&#34;来解决问题。来自yaml。

--- 
tables: 

  trs_fact_txn_detail: 
        expiredinterval: "90d"
        fields: "abc"

  trs_fact_txn_detail_et:  
        expiredinterval: "56d"
        fields: "abcdef"