Java 使用Spring Boot从命令行接收输入
因此,我有一个非常小的Spring Boot命令行应用程序(没有嵌入式tomcat服务器或任何可以使其成为web应用程序的东西),我尝试使用java.util中的Java 使用Spring Boot从命令行接收输入,java,spring,spring-boot,Java,Spring,Spring Boot,因此,我有一个非常小的Spring Boot命令行应用程序(没有嵌入式tomcat服务器或任何可以使其成为web应用程序的东西),我尝试使用java.util中的Scanner类读取用户输入。这根本不起作用。我原以为这在SpringBoot中是非常基本的东西,但我在S.O或教程上的所有搜索都没有结果。最好的办法是什么?或者Spring Boot不是我想要做的 这是我的班级: package test; import org.slf4j.Logger; import org.slf4j.Logg
Scanner
类读取用户输入。这根本不起作用。我原以为这在SpringBoot中是非常基本的东西,但我在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
或者Spring Boot不是我想要做的
你说得对
Spring引导应用程序是一个服务器应用程序(和其他任何.war一样),运行在嵌入式Tomcat中。这里可以是什么样的扫描仪
如果您想与它交互,您应该创建REST控制器并通过端点发送/接收数据。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
}
}
希望这是您的要求。同样,SpringBoot应用程序不能说是真正意义上的交互式应用程序。有很多解释。Spring Boot适用于客户机-服务器体系结构。因此,我找到了一种方法,通过尝试一个普通的gradle应用程序(而不是Spring Boot)使其工作。我遇到了同样的问题,解决方法是在我的
build.gradle
文件中添加一行,以连接默认的stdin
在普通java gradle应用程序中:
run {
standardInput = System.in
}
但在Spring Boot应用程序中,我添加了以下行:
bootRun {
standardInput = System.in
}
原来Spring Boot毕竟可以处理命令行应用程序。我遵循以下方法
@SpringBootApplication
public class Test {
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
}
它对我很有用,你也可以在主类中组合命令行侦听器。可能重复的请看,这不是重复的。没有tomcat或任何其他服务器。它只是一个命令行应用程序(或者至少我希望它是这样)。使它成为web应用程序的每一个依赖都是故意忽略的。我想,只要坚持基本原则就行了。感谢您的回答,您可以使用spring boot创建命令行应用程序(尽管我不确定它是否允许控制台交互),因此它不仅仅适用于服务器应用程序。诚然,对命令行使用Spring引导可能有些过分,但这是可能的(使用
CommandLineRunner
)
@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);
}
}