将数组存储在YAML中。

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

Stores array in YAML

问题

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

  1. report:
  2. interval: 3
  3. to:
  4. - somemail@gmail.com
  5. - somemail@somecompany.com
  6. - io.lab@hh.com
  7. - ...

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

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

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

  1. 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:

  1. report:
  2. interval: 3
  3. to:
  4. - somemail@gmail.com
  5. - somemail@somecompany.com
  6. - io.lab@hh.com
  7. - ...

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

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

So, i'm expecting to get this array:

  1. 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

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

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

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

并在属性文件中:

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

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

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

祝你有个好的一天。

英文:

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

But maybe you can use something like

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

and in the property file:

  1. report:
  2. interval: 3
  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:

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

Good day

答案2

得分: 1

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

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

YML

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

Class

  1. @Value("${report.to:}")
  2. private String[] array;
  3. @Value("#{'${report.to}'.split(',')}")
  4. private List<String> recipients;
  5. @PostConstruct
  6. void testList(){
  7. recipients.stream().forEach(System.out::println);
  8. for (String a : array) {
  9. System.out.println(a);
  10. }
  11. }
英文:
  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

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

Class

  1. Value(&quot;${report.to:}&quot;)
  2. private String[] array;
  3. @Value(&quot;#{&#39;${{report.to}&#39;.split(&#39;,&#39;)}&quot;)
  4. private List&lt;String&gt; recipients;
  5. @PostConstruct
  6. void testList(){
  7. list.stream().forEach(System.out::println);
  8. for (String a : array) {
  9. System.out.println(a);
  10. }
  11. }

答案3

得分: 1

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

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

您的Java代码可以是:

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

或者

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

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

英文:

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

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

Your Java code can be

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

OR

  1. @Value(&quot;${report.to}&quot;)
  2. 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:

确定