Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot YAML: set null property value

I'm having trouble setting null as a property value.

This is how the value is defined in YAML file:

my-property: null

This is how I inject it in code:

@Value("${my-property}")
private String myProperty;

For some reason, Spring keeps injecting an empty string ("") instead of null. Am I missing something or is this an error in Spring?

like image 652
Jardo Avatar asked Jun 20 '26 18:06

Jardo


1 Answers

You can't, but this is not because YAML. YAML supports null as per:

Empty field in yaml

This is because of a Spring processor class, which turns "null" values into empty strings, see here:

https://github.com/spring-projects/spring-framework/issues/19986

like image 95
breakline Avatar answered Jun 23 '26 15:06

breakline



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!