4 回答

TA貢獻1796條經驗 獲得超10個贊
我遵循以下方法,
@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);
}
}
它對我很有用,你也可以在主類中組合命令行監聽器。

TA貢獻1810條經驗 獲得超4個贊
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 適用于客戶端服務器架構。

TA貢獻1966條經驗 獲得超4個贊
還是 Spring Boot 不適合我想做的事情?
你是對的。
Spring Boot 應用程序是一個服務器應用程序(與任何其他 .war 一樣)并在嵌入式 Tomcat 中運行。什么樣的掃描儀在這里可以?
如果你想與之交互——你應該創建 REST 控制器并通過端點發送/接收數據。

TA貢獻1772條經驗 獲得超8個贊
所以我發現了一種通過嘗試普通的 gradle 應用程序(不是 spring boot)來使其工作的方法。我遇到了同樣的問題,解決方案是在我的build.gradle文件中添加一行以連接默認值stdin
在普通的 java gradle 應用程序中:
run {
standardInput = System.in
}
但在 Spring Boot 應用程序中,我添加了以下行:
bootRun {
standardInput = System.in
}
事實證明,Spring Boot 畢竟可以處理命令行應用程序。
添加回答
舉報