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