Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在dropwizard中使用环境变量重写配置时出现问题_Java_Environment Variables_Dropwizard - Fatal编程技术网

Java 在dropwizard中使用环境变量重写配置时出现问题

Java 在dropwizard中使用环境变量重写配置时出现问题,java,environment-variables,dropwizard,Java,Environment Variables,Dropwizard,MyWebConfiguration.java具有以下代码,如drop向导所述 public void initialize(Bootstrap<MyWebConfiguration> bootstrap) { LOG.info("Initializing configuration"); // Enable variable substitution with environment variables bootstrap.setConfigurationS

MyWebConfiguration.java具有以下代码,如drop向导所述

public void initialize(Bootstrap<MyWebConfiguration> bootstrap) {
    LOG.info("Initializing configuration");
    // Enable variable substitution with environment variables
    bootstrap.setConfigurationSourceProvider(
       new SubstitutingSourceProvider(
           bootstrap.getConfigurationSourceProvider(),
             new EnvironmentVariableSubstitutor(false)
       )
   );
}
但当我运行应用程序并进行调试以检查tokenSecret的值时,它会在调试控制台中显示tokenSecret=“${TOKEN_SECRET}”

我尝试更改MyWebConfiguration.java,如下所示:-

bootstrap.setConfigurationSourceProvider(
       new SubstitutingSourceProvider(
           bootstrap.getConfigurationSourceProvider(),
             new EnvironmentVariableSubstitutor(true)// changed false to true
       )
   );
但是现在当我尝试运行程序时,它显示了以下错误

Exception in thread "main" io.dropwizard.configuration.UndefinedEnvironmentVariableException: The environment variable 'TOKEN_SECRET' is not defined; could not substitute the expression '${TOKEN_SECRET}'.
    at io.dropwizard.configuration.EnvironmentVariableLookup.lookup(EnvironmentVariableLookup.java:41)
    at org.apache.commons.lang3.text.StrSubstitutor.resolveVariable(StrSubstitutor.java:726)
    at org.apache.commons.lang3.text.StrSubstitutor.substitute(StrSubstitutor.java:649)
    at org.apache.commons.lang3.text.StrSubstitutor.substitute(StrSubstitutor.java:563)
    at org.apache.commons.lang3.text.StrSubstitutor.replace(StrSubstitutor.java:305)
    at io.dropwizard.configuration.SubstitutingSourceProvider.open(SubstitutingSourceProvider.java:39)
    at io.dropwizard.configuration.YamlConfigurationFactory.build(YamlConfigurationFactory.java:80)
    at io.dropwizard.cli.ConfiguredCommand.parseConfiguration(ConfiguredCommand.java:124)
    at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:72)
    at io.dropwizard.cli.Cli.run(Cli.java:75)
    at io.dropwizard.Application.run(Application.java:79)

有人能告诉我哪里出了问题吗?

您的环境变量没有传播。从您的系统到您启动DropWizard的IDE没有自动传播

其次,在使用
新环境variablesubstitutor(false)
(非严格)时,您需要提供默认值,即使它们应该为空:

tokenSecret: ${TOKEN_SECRET:-}

缺少时,strict将抛出
UndefinedEnvironmentVariableException
,而非strict将使用空字符串。

在何处设置实际的环境变量。在类中,编写main和do:System.out.println(System.getenv(“TOKEN_SECRET”);并查看输出是否为空。你在eclipse中运行吗?是的,我在eclipse中运行它。如果我通过终端运行它,它就会工作。您的eclipse环境不知道您的环境变量。您需要进入运行配置并在其中设置变量,谢谢。这对我来说很有用。我投票结束这个问题,因为这个问题与标题和问题所暗示的完全不同——环境变量从IDE传播,而不是从系统传播。基本上是“为什么这个代码不起作用?”。
tokenSecret: ${TOKEN_SECRET:-}