My Spring Boot project has a handler with the following request parameter:
@RequestParam(value = "release", required = false) Release release
Release is an enum I have defined. I have a method, Release.parse(), which converts Strings to Releases. I register it in a @Configuration class that implements WebMvcConfigurer:
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(String.class, Release.class, Release::parse);
}
This works fine for non-null values, but I want this method to be called even when no query parameter is parsed (so I can map null to Release.DEVELOP).
The documentation for Spring type conversion says that "For each call to convert(S), the source argument is guaranteed to be NOT null."
So how do I set up a converter for when the argument IS null?
n.b. I can't do this using defaultValue in my @RequestParam annotation, as this requires a String (and doesn't allow static values, so I can't just read from my Release.DEVELOP enum).