为了账号安全,请及时绑定邮箱和手机立即绑定

使用 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 关注
  • 340 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号