Spring Boot YAML: set null property value
Asked Answered
D

4

8

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?

Discover answered 23/11, 2020 at 18:53 Comment(1)
Thats simply because 'null' in YAML file undestood as 'null' string, not null value. To get what you want just remove value at all and then use null as default value in EL: @Value("${my-property:null}")Raposa
P
6

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

Potent answered 14/12, 2021 at 11:9 Comment(0)
T
1

Try to use instead of $ use this #. Or try this @Value("${<my-property>}")

Turnbow answered 23/11, 2020 at 18:59 Comment(0)
E
0

I had a similar use case, and had to go through quite a bit of trial and error.

The following two-step solution worked for me

  1. Don't define my-property in the YAML file. This way Spring cannot (annoyingly) inject a stringified value like "" or "~" or "null", and will be forced to look for a default value.

  2. Use #{null} as the default value. This is an SpEL expression that evaluates to null.

In your example, it will be

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

Be careful not to mess up with the curly brackets.

Endurant answered 23/3, 2022 at 16:20 Comment(0)
S
0

You can set null value via EnvironmentPostProcessor (here is a general guide to this thing).

It works like this:

  • You create a property named fake.property.name with text value "null" in your yaml file.
  • Then you read the value of property named fake.property.name in EnvironmentPostProcessor and, if fake.property.name has text value "null", you create and add a new property source containing single property named real.property.name with value null.
// This class is registered in resources/META-INF/spring.factories
public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {

  @Override
  public void postProcessEnvironment(
      ConfigurableEnvironment environment,
      SpringApplication application) {
    final var propertyValue = environment.getProperty("fake.property.name");
    if ("null".equals(propertyValue)) {
      final var fakeMap = new HashMap<String, String>();
      fakeMap.put("real.property.name", null);
      environment
          .getPropertySources()
          .addLast(
              new MapPropertySource(
                  "nullValuePropertySource",
                  fakeMap
              ));
    }
  }
}

Don't forget to create a file src/main/resources/META-INF/spring.factories (if you use Maven or Gradle, otherwise just put a file META-INF/spring.factories inside your .jar) with content

org.springframework.boot.env.EnvironmentPostProcessor=org.example.CustomEnvironmentPostProcessor 

Somali answered 5/8, 2024 at 2:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.