I have a class Outcome
, one of whose fields is an instance of Schema
. But because the latter might be null, I have defined Outcome
's getSchema()
to return the value wrapped in Vavr's Option
. In my Spring Boot 2 application the controller method that handles updating Outcome
s has a parameter @Valid Outcome outcome
. However when attempting to populate this parameter from the form data Spring's conversion service flags up the following error:
Cannot convert value of type 'java.lang.String' to required type 'io.vavr.control.Option' for property 'schema': no matching editors or conversion strategy found
That is, it has been unable to map a string identifier such as '1234' to the existing Schema
instance with that Long
identifier and then set the Outcome
instance's schema
field to that. This is despite the fact that in my WebMvcConfigurer
class I have added to Spring's conversion service Spring Data's QueryExecutionConverters
, which are claimed to handle converting Vavr Optional
:
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.repository.util.QueryExecutionConverters;
...
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean(name="conversionService")
public ConversionService getConversionService() {
ConfigurableConversionService conversionService = new DefaultConversionService();
QueryExecutionConverters.registerConvertersIn(conversionService);
System.out.println("Registered QueryExecutionConverters");
return conversionService;
}
...
If I change Outcome
's getSchema()
to return Java 8's Optional<Schema>
instead then the schema field is successfully set for the Outcome
instance passed to my controller method.