首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Yaml中的映射列表到Spring中的对象列表

Yaml中的映射列表到Spring中的对象列表
EN

Stack Overflow用户
提问于 2015-09-15 18:21:46
回答 6查看 135.6K关注 0票数 72

在我的Spring应用程序中,我有包含以下内容的application.yaml配置文件。我希望将其作为配置对象注入,并列出信道配置:

代码语言:javascript
运行
复制
available-payment-channels-list:
  xyz: "123"
  channelConfigurations:
    -
      name: "Company X"
      companyBankAccount: "1000200030004000"
    -
      name: "Company Y"
      companyBankAccount: "1000200030004000"

并使用PaymentConfiguration对象列表填充@Configuration对象:

代码语言:javascript
运行
复制
    @ConfigurationProperties(prefix = "available-payment-channels-list")
    @Configuration
    @RefreshScope
    public class AvailableChannelsConfiguration {

        private String xyz;

        private List<ChannelConfiguration> channelConfigurations;

        public AvailableChannelsConfiguration(String xyz, List<ChannelConfiguration> channelConfigurations) {
            this.xyz = xyz;
            this.channelConfigurations = channelConfigurations;
        }

        public AvailableChannelsConfiguration() {

        }

        // getters, setters


        @ConfigurationProperties(prefix = "available-payment-channels-list.channelConfigurations")
        @Configuration
        public static class ChannelConfiguration {
            private String name;
            private String companyBankAccount;

            public ChannelConfiguration(String name, String companyBankAccount) {
                this.name = name;
                this.companyBankAccount = companyBankAccount;
            }

            public ChannelConfiguration() {
            }

            // getters, setters
        }

    }

我将它作为一个普通bean注入@Autowired构造函数。正确填充了xyz的值,但是当Spring试图将yaml解析为对象列表时,我将得到

代码语言:javascript
运行
复制
   nested exception is java.lang.IllegalStateException: 
    Cannot convert value of type [java.lang.String] to required type    
    [io.example.AvailableChannelsConfiguration$ChannelConfiguration] 
    for property 'channelConfigurations[0]': no matching editors or 
    conversion strategy found]

有什么线索吗这里有什么问题吗?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2015-09-16 09:18:28

原因肯定是在别的地方。只使用SpringBoot1.2.2开箱即用,没有配置,它就能工作了。看看这个回购品-你能让它坏掉吗?

https://github.com/konrad-garus/so-yaml

你确定YAML文件看起来和你粘贴的一模一样吗?没有额外的空格,字符,特殊字符,错误缩进之类的东西?是否有可能在搜索路径的其他地方有另一个文件被使用,而不是您所期望的文件?

票数 21
EN

Stack Overflow用户

发布于 2015-09-15 18:41:10

  • 你不需要构造函数
  • 不需要对内部类进行注释
  • RefreshScope在与@Configuration一起使用时存在一些问题。请参阅这个github问题

像这样改变你的班级:

代码语言:javascript
运行
复制
@ConfigurationProperties(prefix = "available-payment-channels-list")
@Configuration
public class AvailableChannelsConfiguration {

    private String xyz;
    private List<ChannelConfiguration> channelConfigurations;

    // getters, setters

    public static class ChannelConfiguration {
        private String name;
        private String companyBankAccount;

        // getters, setters
    }

}
票数 18
EN

Stack Overflow用户

发布于 2016-10-20 12:04:53

我参考了这篇文章和许多其他文章,没有找到一个清晰、简洁的帮助回答。我提供了我的发现,从这个帖子中得到了一些参考,如下所示:

Spring版本:1.3.5 version

Spring-核心版本:4.2.6

依赖关系管理: Brixton.SR1

以下是相关的yaml摘录:

代码语言:javascript
运行
复制
tools:
  toolList:
    - 
      name: jira
      matchUrl: http://someJiraUrl
    - 
      name: bamboo
      matchUrl: http://someBambooUrl

我创建了一个Tools.class:

代码语言:javascript
运行
复制
@Component
@ConfigurationProperties(prefix = "tools")
public class Tools{
    private List<Tool> toolList = new ArrayList<>();
    public Tools(){
      //empty ctor
    }

    public List<Tool> getToolList(){
        return toolList;
    }

    public void setToolList(List<Tool> tools){
       this.toolList = tools;
    }
}

我创建了一个Tool.class:

代码语言:javascript
运行
复制
@Component
public class Tool{
    private String name;
    private String matchUrl;

    public Tool(){
      //empty ctor
    }

    public String getName(){
        return name;
    }

    public void setName(String name){
       this.name= name;
    }
    public String getMatchUrl(){
        return matchUrl;
    }

    public void setMatchUrl(String matchUrl){
       this.matchUrl= matchUrl;
    }

    @Override
    public String toString(){
        StringBuffer sb = new StringBuffer();
        String ls = System.lineSeparator();
        sb.append(ls);
        sb.append("name:  " + name);
        sb.append(ls);
        sb.append("matchUrl:  " + matchUrl);
        sb.append(ls);
    }
}

我在另一堂课中使用了这个组合,通过@Autowired

代码语言:javascript
运行
复制
@Component
public class SomeOtherClass{

   private Logger logger = LoggerFactory.getLogger(SomeOtherClass.class);

   @Autowired
   private Tools tools;

   /* excluded non-related code */

   @PostConstruct
   private void init(){
       List<Tool>  toolList = tools.getToolList();
       if(toolList.size() > 0){
           for(Tool t: toolList){
               logger.info(t.toString());
           }
       }else{
           logger.info("*****-----     tool size is zero     -----*****");
       }  
   }

   /* excluded non-related code */

}

在我的日志里名字和匹配的网址都被记录下来了。这是在另一台机器上开发的,因此我不得不重新键入上述所有内容,所以如果我无意中输入错误,请提前原谅我。

我希望这个合并的评论对很多人都有帮助,我也感谢以前对这个帖子的贡献者!

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32593014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档