账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    yml配置文件数据转换成list出现类型转换异常
    42
    0

    Spring Boot项目

    yml中配置

    acme:
      list[0]:
         name:my name
         description:my description

    java代码

    @ConfigurationProperties("acme")
    @Component
    public class AcmeProperties {
        private final List<User> list = new ArrayList<>();
    
        public List<User> getList(){
            return this.list;
        }
    }

    user类

    @Repository
    public class User {
    
        private String name;
        private String description;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    }
    

    调用

    @RestController
    @SpringBootApplication
    public class DemoApplication {
        private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);
    
        @Autowired
        private AcmeProperties acmeProperties;
        
        @RequestMapping("/proper")
        String proper(){
            List<User> list = acmeProperties.getList();
            for(User user : list){
              logger.info(user.getName()+":"+user.getDescription());
            }
            return "hello world";
        }
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }

    启动报错:

    Failed to bind properties under 'acme.list[0]' to com.example.demo.dao.User:
    
        Reason: No converter found capable of converting from type [java.lang.String] to type [com.example.demo.dao.User]
    
    Action:
    
    Update your application's configuration

    有大佬知道配置文件哪里写错了吗?

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部