How to avoid truncating zero leading numbers when pasting using Spring @Value annotations?

Let's say we have an environment variable exported as:

export SOME_STRING_CONFIG_PARAM="01"

and application.propertieswith:

some.string.config.param=${SOME_STRING_CONFIG_PARAM:01}

entered in the Java field:

@Value("${some.string.config.param}")
String someStringConfigfParam;

After loading the Spring application, print the field:

System.out.println("someStringConfigParam: " + someStringConfigParam);

result:

someStringConfigParam: 1

How to tell Spring that the value should be treated as a string?

+4
source share
1 answer

I get the correct answer for your demo project:

This is the Test Property Value = 01

But I get a problem in the file yml, the reason is to ymltreat it 01as a default integer. Just use double quotes to solve this problem.

Enter the examples in the ymlfile:

a: 123                     # an integer
b: "123"                   # a string, disambiguated by quotes
c: 123.0                   # a float
d: !!float 123             # also a float via explicit data type prefixed by (!!)
e: !!str 123               # a string, disambiguated by explicit type
f: !!str Yes               # a string via explicit type
g: Yes                     # a boolean True (yaml1.1), string "Yes" (yaml1.2)
h: Yes we have No bananas  # a string, "Yes" and "No" disambiguated by context.

. : https://en.wikipedia.org/wiki/YAML
Yaml?

+2

All Articles