使用 Spring Boot 从命令行接收输入

所以我有一个非常小的 Spring Boot 命令行应用程序(没有嵌入式 tomcat 服务器或任何可以使它成为 Web 应用程序的东西)和一个类,我尝试使用Scannerjava.util 中的类来读取用户的输入。这根本不起作用。我认为这将是 Spring Boot 中非常基本的东西,但我在 SO 或教程上的所有搜索都没有结果。最好的方法是什么?还是 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

幕布斯6054654
浏览 341回答 4
4回答

白衣染霜花

我遵循以下方法,@SpringBootApplicationpublic class Test {    public static void main(String[] args) {        SpringApplication.run(Test.class, args);    }}@Componentpublic 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);     }}它对我很有用,你也可以在主类中组合命令行监听器。

蝴蝶不菲

Spring Boot 不是为任何类型的通用用途而设计的。它有特定的目的。我们不应该总是考虑如何使用 Spring Boot 来满足任何一种需求。我知道 Spring Boot 已经变得非常流行。对于您的问题,如果您想创建交互式应用程序/程序,则必须以简单的方式进行。我只是在代码片段下方提供。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 适用于客户端服务器架构。

慕标5832272

还是 Spring Boot 不适合我想做的事情?你是对的。Spring Boot 应用程序是一个服务器应用程序(与任何其他 .war 一样)并在嵌入式 Tomcat 中运行。什么样的扫描仪在这里可以?如果你想与之交互——你应该创建 REST 控制器并通过端点发送/接收数据。

料青山看我应如是

所以我发现了一种通过尝试普通的 gradle 应用程序(不是 spring boot)来使其工作的方法。我遇到了同样的问题,解决方案是在我的build.gradle文件中添加一行以连接默认值stdin在普通的 java gradle 应用程序中:run {    standardInput = System.in}但在 Spring Boot 应用程序中,我添加了以下行:bootRun {    standardInput = System.in}事实证明,Spring Boot 毕竟可以处理命令行应用程序。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java