如何在spring boot的application.yml中定义空列表

gupuwyp2  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(487)

我的spring boot项目中有application.yml,希望声明空列表。我的意思是有两级配置层次结构。获取application.yml中声明的值需要此类

@Configuration
@ConfigurationProperties("book")
public class PropertiesReader {

    private List<Long> authors;

    public List<Long> getAuthors() {
        return new ArrayList<>(authors);
    }

    public void setAuthors(List<Long> authors) {
        this.authors= new ArrayList<>(authors);
    }

这就是application.yml的一部分

book:
   library: ${LIBRARY_REFERENCE:library}
   secondValue:
      expirationAfter: 10
   authors: ${AUTHORS_IDS:[]}

所以花括号中的值在另一个外部配置文件中声明。但若并没有声明值,spring应该分配在冒号符号后面声明的默认值。在单值中它是有效的,但在list中,如果我如上所述声明它,我会得到numberformatexception,因为value[]被分配给list。如何写好?
在外部配置中,它应该如何编写?如果有时我想声明值,但有时它必须是空的

name: AUTHORS_IDS
      value:
zaq34kh6

zaq34kh61#

你只需要放一个冒号。我就这样解决了我的问题,很管用。

book:
   authors: ${AUTHORS_IDS:}

相关问题