[英]Receive input from command line with Spring Boot
因此,我有一個非常小的Spring Boot命令行應用程序(沒有嵌入式tomcat服務器或任何使其成為Web應用程序的應用程序),並且只有一個類,我嘗試使用java.util中的Scanner
類讀取用戶的輸入。 這根本不起作用。 我以為這是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
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 Boot應用程序是一個服務器(與其他.war一樣),並且在嵌入式Tomcat中運行。 這里可以使用哪種掃描儀?
如果要與之交互,則應創建REST控制器並通過端點發送/接收數據。
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適用於客戶端服務器架構。
因此,我發現了一種通過嘗試正常的gradle應用程序(而非Spring Boot)使其工作的方法。 我遇到了同樣的問題,解決方法是在build.gradle
文件中添加一行以build.gradle
默認的stdin
在普通的Java gradle應用程序中:
run {
standardInput = System.in
}
但是在Spring Boot應用程序中,我添加了以下行:
bootRun {
standardInput = System.in
}
事實證明,Spring Boot畢竟可以處理命令行應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.