【问题标题】:Receive input from command line with Spring Boot使用 Spring Boot 从命令行接收输入
【发布时间】:2019-05-29 10:36:05
【问题描述】:

所以我有一个非常小的 Spring Boot 命令行应用程序(没有嵌入式 tomcat 服务器或任何可以使它成为 Web 应用程序的东西) 有一个类,我尝试使用 @987654323 @class from java.util 读取用户的输入。这根本不起作用。我认为这将是 Spring Boot 中非常基本的东西,但我在 S.O 或教程上的所有搜索都没有产生任何结果。最好的方法是什么?还是 Spring Boot 不适合我想做的事情?

这是我的课:

package test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Scanner;

@SpringBootApplication
public class MyApplication implements CommandLineRunner {

    private static Logger LOG = LoggerFactory
        .getLogger(MyApplication.class);

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.run(args);
    }

    @Override
    public void run(String... args) throws Exception {
        LOG.info("EXECUTING : command line runner");

        Scanner in = new Scanner(System.in);

        System.out.println("What is your name?");
        String name = in.next();
        System.out.println("Hello " + name + " welcome to spring boot" );
    }
}

抛出的异常是:

2019-05-29 11:31:29.116  INFO 4321 --- [           main] test.MyApplication  : EXECUTING : command line runner
2019-05-29 11:31:29.119  INFO 4321 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-05-29 11:31:29.124 ERROR 4321 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
        at com.jaletechs.png.PrimeNumberGeneratorApplication.main(MyApplication.java:19) [main/:na]
Caused by: java.util.NoSuchElementException: null
        at java.util.Scanner.throwFor(Scanner.java:862) ~[na:1.8.0_201]
        at java.util.Scanner.next(Scanner.java:1371) ~[na:1.8.0_201]
        at test.MyApplication.run(MyApplication.java:27) [main/:na]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
        ... 3 common frames omitted

【问题讨论】:

标签: java spring spring-boot


【解决方案1】:

我遵循以下方法,

@SpringBootApplication
public class Test {
    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }
}

@Component
public class MyRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Enter word!");
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        System.out.println(line);
     }
}

它对我很有效,你也可以在主类中结合命令行监听器。

【讨论】:

    【解决方案2】:

    Spring Boot 不是为任何通用用途而设计的。它有特定的目的。我们不应该总是考虑如何将 Spring Boot 用于任何类型的需求。我知道 Spring Boot 已经变得非常流行。对于你的问题,如果你想创建一个交互式应用程序/程序,你必须以一种简单的方式来做。我只是在下面提供代码sn-p。

    public class Interactive
    {
    
      public static void main (String[] args)
      {
        // create a scanner so we can read the command-line input
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your name ? ");
        String username = scanner.next();
        System.out.print("Enter your age ? ");
        int age = scanner.nextInt();
    
        //Print all name, age etc
      }
    
    }
    

    希望这可能是您的要求。同样,Spring Boot 应用程序不能说是真正意义上的交互式。有很多解释。 Spring Boot 适用于客户端服务器架构。

    【讨论】:

    • 只要坚持我猜的基础。感谢您的回答
    【解决方案3】:

    还是 Spring Boot 不适合我想做的事情?

    你是对的。

    Spring Boot 应用程序是一个服务器应用程序(与任何其他 .war 类似)并在嵌入式 Tomcat 中运行。这里的 Scanner 可以是什么类型的?

    如果你想与之交互 - 你应该创建 REST 控制器并通过端点发送/接收数据。

    【讨论】:

    • 您可以使用spring boot创建命令行应用程序(虽然我不确定它是否允许控制台交互),因此它不仅适用于服务器应用程序。当然,在命令行中使用 Spring Boot 可能有点矫枉过正,但有可能(使用 CommandLineRunner
    【解决方案4】:

    所以我发现了一种通过尝试普通的 gradle 应用程序(不是 spring boot)来使其工作的方法。我遇到了同样的问题,解决方案是在我的build.gradle 文件中添加一行来连接默认的stdin

    在普通的 java gradle 应用程序中:

    run {
        standardInput = System.in
    }
    

    但在 Spring Boot 应用程序中,我添加了以下行:

    bootRun {
        standardInput = System.in
    }
    

    事实证明 Spring Boot 毕竟可以处理命令行应用程序。

    【讨论】:

      猜你喜欢
      • 2020-10-29
      • 1970-01-01
      • 2023-01-05
      • 2019-12-08
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多