如何在Spring shell中使用Kotlin传递varargs

fdbelqdn  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(159)

我正在尝试编写一个Spring shell应用程序并传递一些var参数。基本上我希望传递
fetch FA-207542 FA-207984 FA-211258 FA-202298此处的文档仍待定

  1. @ShellMethod(value = "fetches all pic related data for processing")
  2. fun fetch(vararg tickets: String) {
  3. println("number of tickets is ${tickets.size}")
  4. }

成果

  1. shell:>fetch FA-207542 FA-207984 FA-211258 FA-202298
  2. number of tickets is 1

我尝试使用StringArray,但出现错误:

  1. fun fetch(tickets: StringArray) {
  2. println("number of tickets is ${tickets.size()}")
  3. }

错误:

  1. shell:>fetch FA-207542 FA-207984 FA-211258 FA-202298
  2. Parameter specified as non-null is null: method FetchCommand.fetch, parameter tickets
  3. Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.
4ktjp1zp

4ktjp1zp1#

使用java(sry),我可以实现以下功能:

  1. @ShellComponent
  2. public class MyCommands {
  3. @ShellMethod
  4. public String fetch(String... tickets) {
  5. return String.format("Number of tickets is %d.", tickets.length);
  6. }
  7. }

然后:

  1. shell:>fetch --tickets FA-207542 FA-207984 FA-211258 FA-202298
  2. Number of tickets is 4.
  3. shell:>

使用以下产品可获得相同的结果:分别为@ShellCommand(arity = -1)arity = Integer.MAX_VALUE。#

展开查看全部

相关问题