将数组存储在YAML中。

huangapple go评论91阅读模式
英文:

Stores array in YAML

问题

如何在 YAML 中存储数组,并在代码中从该数组中提取值?我尝试了以下方法:

report:
  interval: 3
  to:
    - somemail@gmail.com
    - somemail@somecompany.com
    - io.lab@hh.com
    - ...

稍后在代码中(Spring Boot 中),我试图将值提取到我的数组中:

@Value("${report.to}")
private String[] recipients;

因此,我期望获得这个数组:

String[] recipients = new String[]{"somemail@gmail.com", ...};

但是我得到了异常:Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'report.to' in value "${report.to}"

我做错了什么?

英文:

How to store array in yaml and extract values from this array in the code? I tried like this:

report:
  interval: 3
  to:
    - somemail@gmail.com
    - somemail@somecompany.com
    - io.lab@hh.com
    - ...

Later in the code (Spring Boot) I'm trying to extract values into my array:

@Value("${report.to}")
private String [] recipients;

So, i'm expecting to get this array:

String [] recipients = new String[]{"somemail@gmail.com", ...}

But I get exception: Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'report.to' in value "${report.to}"

What did i do wrong?

答案1

得分: 1

只是建议一下,如果不是你想要的,请随意忽略,不用担心。

但或许你可以使用类似这样的方式:

    @Value("#{ '${report.to}'.split(',') }")
    private List<String> recipients;

并在属性文件中:

report:
  interval: 3
  to: somemail@gmail.com,somemail@somecompany.com,io.lab@hh.com

如果你在应用程序中获取了列表,转换成数组应该很简单。就像这样:

    public String[] getRecipients() {
        return recipients.toArray(new String[0]);
    }

祝你有个好的一天。

英文:

Just suggesting, if it is not what you want, please feel free to ignore, no worries.

But maybe you can use something like

    @Value(&quot;#{&#39;${report.to}&#39;.split(&#39;,&#39;)}&quot;)
    private List&lt;String&gt;         recipients;

and in the property file:

report:
  interval: 3
  to: somemail@gmail.com,somemail@somecompany.com,io.lab@hh.com

If you get the list in your app, the conversion to array should be straightforward. Like:

    public String[] getRecipients() {
        return recipients.toArray(new String[0]);
    }

Good day

答案2

得分: 1

  1. 一种方法是将元素作为分隔列表传递。通常我们使用逗号,对于字符串数组,它可以直接使用。

  2. 第二种方法是使用List,然后您需要使用Spring SPEL格式设置分隔符... 请参见下面的示例。

YML

report:
  interval: 3
  to: somemail@gmail.com,somemail@somecompany.com,io.lab@hh.com

Class

@Value("${report.to:}")
private String[] array;

@Value("#{'${report.to}'.split(',')}")
private List<String> recipients;

@PostConstruct
void testList(){
    recipients.stream().forEach(System.out::println);
    for (String a : array) {
        System.out.println(a);
    }
}
英文:
  1. One way is to pass the elements as delimited list. Typically we have used comma and it works out of the box for String arrays.

  2. Second way to use a List, then you will need to set the delimiter using Spring SPEL format... see example below.

YML

report:
  interval: 3
  to: somemail@gmail.com,somemail@somecompany.com,io.lab@hh.com

Class

Value(&quot;${report.to:}&quot;)
private String[] array;


@Value(&quot;#{&#39;${{report.to}&#39;.split(&#39;,&#39;)}&quot;)
private List&lt;String&gt; recipients;

@PostConstruct
void testList(){
    list.stream().forEach(System.out::println);
    for (String a : array) {
        System.out.println(a);
    }
}

答案3

得分: 1

将您的YAML字段用逗号分隔以将其解析为数组:

report:
  interval: 3
  to: somemail@gmail.com, somemail@somecompany.com, io.lab@hh.com

您的Java代码可以是:

@Value("${report.to}")
private String[] recipients;

或者

@Value("${report.to}")
private List<String> recipients;

查看此链接以获取更多提示和技巧:
https://youtu.be/NFQDqEhx2e0

英文:

Make your yaml fields comma-separated to parse it as array

report:
  interval: 3
  to: somemail@gmail.com,somemail@somecompany.com,io.lab@hh.com

Your Java code can be

@Value(&quot;${report.to}&quot;)
private String [] recipients;

OR

@Value(&quot;${report.to}&quot;)
private List&lt;String&gt; recipients;

Follow this link for more tips and tricks
https://youtu.be/NFQDqEhx2e0

huangapple
  • 本文由 发表于 2020年10月8日 15:35:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64257830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定