qq_41027559
哥斯拉不会说话
采纳率0%
2021-03-03 22:26

yaml文件怎么向Map<String, List<Pet>> allPets注入参数?

最近在B站跟着狂神学SpringBoot,在第20集的时候碰上了给Map<String, List<Pet>> allPets这个变量绑定数据,用不同格式试了好多遍都出错,求大牛解答。(附Pet类和出错信息)

package com.atguigu.boot.bean;

//Pet类

public class Pet {
    private String name;
    private Double weight;
}


 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-03 22:19:22.868 ERROR 2660 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target [Bindable@622d40d7 type = java.util.List<com.atguigu.boot.bean.Pet>, value = 'none', annotations = array<Annotation>[[empty]]] failed:

    Property: person.allpets.sick[0].name
    Value: tom
    Origin: class path resource [application.yaml] - 25:16
    Reason: The elements [person.allpets.sick[0].name,person.allpets.sick[1].name,person.allpets.sick[1].weight] were left unbound.
    Property: person.allpets.sick[1].name
    Value: jerry
    Origin: class path resource [application.yaml] - 26:16
    Reason: The elements [person.allpets.sick[0].name,person.allpets.sick[1].name,person.allpets.sick[1].weight] were left unbound.
    Property: person.allpets.sick[1].weight
    Value: 47
    Origin: class path resource [application.yaml] - 26:30
    Reason: The elements [person.allpets.sick[0].name,person.allpets.sick[1].name,person.allpets.sick[1].weight] were left unbound.

Action:

Update your application's configuration
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • vbhfdghff hweiyu00 1月前

    配置类:

    @Component
    @ConfigurationProperties(prefix = "test")
    public class MyConfig {
    
        private Map<String, List<Pet>> data;
    
        public Map<String, List<Pet>> getData() {
            return data;
        }
    
        public void setData(Map<String, List<Pet>> data) {
            this.data = data;
        }
    
        public static class Pet {
    
            private String name;
    
            private Double weight;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public Double getWeight() {
                return weight;
            }
    
            public void setWeight(Double weight) {
                this.weight = weight;
            }
        }
    }

    配置文件:

    test:
      data:
        key1:
          - name: name1
            weight: 1
          - name: name2
            weight: 2
        key2:
          - name: name3
            weight: 3
          - name: name4
            weight: 4
    点赞 评论 复制链接分享
  • weixin_42534024 何必要如此 1月前

    试试现在你的yml文件里面定义出你的list

    pets[0]:

       name: xxx

        weight:1.11

    pets[1]

       name: xxx

        weight:1.11

    这样你就定义出了一个list

    然后再定义你的map 这时候配置list的时候使用 list: ${pets}   

    我没试过是个猜想  如果题主试了能成功麻烦告诉我一下、

    点赞 评论 复制链接分享