【问题标题】:Passing multiple arguments in command line Spring Boot在命令行 Spring Boot 中传递多个参数
【发布时间】:2020-08-06 20:28:22
【问题描述】:

我需要将多个参数传递给 maven 命令行来运行 Spring Boot 应用程序。这就是我在 Spring Boot 中传递命令行参数的方式。我使用的是spring boot 2.2.6 Release

mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8999,--spring.application.instance_id=dhn"

但是我得到以下错误

nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'server.port' to java.lang.Integer

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'server.port' to java.lang.Integer:

    Property: server.port
    Value: 8999,--spring.application.instance_id=dhn
    Origin: "server.port" from property source "commandLineArgs"
    Reason: failed to convert java.lang.String to java.lang.Integer

Action:

Update your application's configuration

好像参数解析不正确

【问题讨论】:

    标签: java spring-boot maven command-line arguments


    【解决方案1】:

    , 分隔符似乎不起作用。虽然我已经在教程中看到过这种风格。

    有效的是将空格作为分隔符:

    mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8999 --spring.application.instance_id=dhn"
    

    【讨论】:

    • 空格分隔符不适用于 Spring-boot 2.1.5
    【解决方案2】:

    对于 Maven Command-Line Arguments,您可以使用 -Dspring-boot.run.arguments 传递参数,而无需将其括在双引号中,例如:

    mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8999,--spring.application.instance_id=dhn

    为了传递Gradle Command-Line Arguments,首先将build.gradle文件中的bootRun任务配置为:

    bootRun {
        if (project.hasProperty('args')) {
            args project.args.split(',')
        }
    }
    

    现在将命令行参数传递为:

    ./gradlew bootRun -pargs=--server.port=8999,--spring.application.instance_id=dhn

    请参阅此快速教程 - Command-Line Arguments in Spring Boot 以获得广泛的理解。

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 1970-01-01
      • 2017-11-12
      • 2012-11-27
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2019-06-23
      相关资源
      最近更新 更多