简体   繁体   English

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

[英]Receive input from command line with Spring Boot

So I have a really small Spring Boot command line application (There is no embedded tomcat server or anything that would make it a web application) with a single class where I try to use the Scanner class from java.util to read input from the user. 因此,我有一个非常小的Spring Boot命令行应用程序(没有嵌入式tomcat服务器或任何使其成为Web应用程序的应用程序),并且只有一个类,我尝试使用java.util中的Scanner类读取用户的输入。 This does not work at all. 这根本不起作用。 I thought it would be pretty basic stuff in Spring Boot, but all of my searches both on SO or tutorials have yielded no results. 我以为这是Spring Boot中非常基本的东西,但是我在SO或教程上的所有搜索都没有结果。 What's the best way to go about it? 最好的方法是什么? or is Spring Boot just not for what I want to do? 还是Spring Boot不能满足我的需求?

Here's my class: 这是我的课:

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" );
    }
}

The exception being thrown is: 抛出的异常是:

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

or is Spring Boot just not for what I want to do? 还是Spring Boot不能满足我的需求?

You are right. 你是对的。

Spring Boot application is a server one (like any other .war) and runs in an embedded Tomcat. Spring Boot应用程序是一个服务器(与其他.war一样),并且在嵌入式Tomcat中运行。 What kind of Scanner here can be? 这里可以使用哪种扫描仪?

If you want to interact with it - you should create REST controller and send/receive data through endpoints. 如果要与之交互,则应创建REST控制器并通过端点发送/接收数据。

Spring boot is not designed for any kind of general purpose usages. Spring Boot并非为任何通用用途而设计。 It has specific purposes. 它有特定的用途。 We should not always think about how to use Spring Boot for any kind of requirements. 我们不应该总是考虑如何将Spring Boot用于任何类型的需求。 I know Spring Boot has become very popular. 我知道Spring Boot已变得非常流行。 To your question, if you want to create an interactive application/program, you have to do in a simple manner. 对于您的问题,如果要创建交互式应用程序/程序,则必须以简单的方式进行。 I just provide below the code snippet. 我只在下面的代码片段中提供。

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
  }

}

Hope this may be your requirement. 希望这可能是您的要求。 Again Spring Boot application can not be stated as interactive in true sense. 同样,Spring Boot应用程序不能说是真正意义上的交互式。 Many interpretations are there. 有很多解释。 Spring Boot is suitable for client server architecture. Spring Boot适用于客户端服务器架构。

So I discovered a way to make it work by trying out a normal gradle application (not spring boot). 因此,我发现了一种通过尝试正常的gradle应用程序(而非Spring Boot)使其工作的方法。 I ran into the same problem, and the solution was to add a line in my build.gradle file to wire up the default stdin 我遇到了同样的问题,解决方法是在build.gradle文件中添加一行以build.gradle默认的stdin

In a normal java gradle application: 在普通的Java gradle应用程序中:

run {
    standardInput = System.in
}

but in the Spring Boot application, I added the following line: 但是在Spring Boot应用程序中,我添加了以下行:

bootRun {
    standardInput = System.in
}

Turns out Spring Boot could handle a Command Line Application after all. 事实证明,Spring Boot毕竟可以处理命令行应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM