亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Spring Boot 從命令行接收輸入

使用 Spring Boot 從命令行接收輸入

幕布斯6054654 2022-12-21 14:40:16
所以我有一個非常小的 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;@SpringBootApplicationpublic 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 runner2019-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
查看完整描述

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

     }

}

它對我很有用,你也可以在主類中組合命令行監聽器。


查看完整回答
反對 回復 2022-12-21
?
蝴蝶不菲

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 適用于客戶端服務器架構。


查看完整回答
反對 回復 2022-12-21
?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

還是 Spring Boot 不適合我想做的事情?

你是對的。

Spring Boot 應用程序是一個服務器應用程序(與任何其他 .war 一樣)并在嵌入式 Tomcat 中運行。什么樣的掃描儀在這里可以?

如果你想與之交互——你應該創建 REST 控制器并通過端點發送/接收數據。


查看完整回答
反對 回復 2022-12-21
?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

所以我發現了一種通過嘗試普通的 gradle 應用程序(不是 spring boot)來使其工作的方法。我遇到了同樣的問題,解決方案是在我的build.gradle文件中添加一行以連接默認值stdin


在普通的 java gradle 應用程序中:


run {

    standardInput = System.in

}

但在 Spring Boot 應用程序中,我添加了以下行:


bootRun {

    standardInput = System.in

}

事實證明,Spring Boot 畢竟可以處理命令行應用程序。


查看完整回答
反對 回復 2022-12-21
  • 4 回答
  • 0 關注
  • 339 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號