4

FYI: I am using Intellij's 'Run/Debug Configurations' tool.

I would like to pass an empty string as a property to my app via the command line:

--spring.ldap.username=

I have tried:

--spring.ldap.username= # results in parse error
--spring.ldap.username=''
--spring.ldap.username="" # results in parse error
--spring.ldap.username=\"\"

The attempts that actually parsed successfully yielded incorrect values, as demonstrated when I try to print the 'empty' Strings:

System.out.println(environment.getProperty("spring.ldap.username"));

// returns:
// ''
// ""

Setting the same property to an empty string in the application.properties file works perfectly:

spring.ldap.username=

The same print statement:

// returns:
//
// ^^ totally empty string

Is there a trick I am missing?

scottysseus
  • 1,922
  • 3
  • 25
  • 50
  • Can you enter them via `Environment variables` in the Run/Debug configuration for the application? – vikingsteve Mar 08 '18 at 12:33
  • That might work, but I want to pass these arguments via the command line. – scottysseus Mar 08 '18 at 16:20
  • You may be overthinking this - wouldn't just defaulting the value of that parameter to an empty string in-code suffice? Is there a specific need to ingest the empty string as a parameter? – kolossus Mar 10 '18 at 22:51
  • I think it would simplify code elsewhere in my system if passing an empty string (a sensible value in this case) to the flag doesn't break things. That being said, I will probably go with your suggestion for now. – scottysseus Mar 12 '18 at 13:53

2 Answers2

3

Try this: --spring.ldap.username (if you leave the =, it will parse as null)

3

So as it turns out, this can be accomplished by using regular JVM arguments.

Instead of

--my.property=

use

-Dmy.property=

Native JVM arguments are able to accept empty Strings I guess.

In IntelliJ, you can add them in the respective fields in the Run/Debug Configurations window as shown below:

enter image description here

This answer to another question also has some more information on passing JVM arguments to Spring applications.

scottysseus
  • 1,922
  • 3
  • 25
  • 50