【问题标题】:loading springboot properties from a Json file using -Dspring.application.json使用 -Dspring.application.json 从 Json 文件加载 springboot 属性
【发布时间】:2022-01-10 08:08:16
【问题描述】:
我正在尝试使用以下命令运行 springboot 应用程序并使用 -Dspring.application.json 传递属性
*mvnw spring-boot:run -Dspring.application.json = "{"Api": {"gateway": {"password": "abc"}}}"*
我尝试使用单引号而不是双引号:
*mvnw spring-boot:run -Dspring.application.json = '{"Api": {"gateway": {"password": "abc"}}}'*
抛出错误:
在当前项目和插件组中找不到前缀“{Api”的插件。
如何解决此错误?我在这里有什么遗漏吗?
【问题讨论】:
标签:
java
json
spring-boot
command-line-arguments
spring-boot-configuration
【解决方案1】:
如果你想使用 Spring-Boot Maven 插件启动你的应用程序,你不能在这里直接使用spring.application.json 参数。您必须使用插件的 spring-boot.run.arguments 参数将参数传递给您的应用程序。
方法如下:
mvnw spring-boot:run -Dspring-boot.run.arguments="--spring.application.json='{\"Api\": {\"gateway\": {\"password\": \"abc\"}}}'"
如您所见,您还必须对双引号进行分隔,以免它们被解释为命令行参数的分隔符。
也许您已经知道并且需要使用 json,但是有一种更简单的方法可以实现这一点:
mvnw spring-boot:run -Dspring-boot.run.arguments="--Api.gateway.password='abc'"