Spring Boot converter not working for configuration properties

193 views Asked by At

I am attempting to provide a Converter to convert a r,g,b tuple in application.yml into a Color object. The property resolution fails with: "org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.awt.Color]"

The Converter is declared like this:

@Component
public static class ColorConverter implements Converter<String, Color>{
    @Override
    public Color convert(String source) {
      ...
    }
}      

My environment:

Spring Boot 2.7

Application annotations;

@SpringBootApplication(scanBasePackages = {"my.packages"})
@EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
@EnableSpringConfigured

The application does have a WebMvcConfigurer sub-class

What else I've tried:

  1. Adding @EnableConfigurationProperties to the application class. This makes no difference.
  2. Confirming that the converter registers during launch (I put a breakpoint on the zero arg constructor)
  3. Registering the converter in the WebMvcConfigurer (this doesn't make sense - the converter I need is on property resolution, not mvc calls - but I tried it anyway). This makes no difference.
1

There are 1 answers

0
Kevin Day On

Finally figured out the right google search terms.

The solution was to annotate the Convert class with @ConfigurationPropertiesBinding:

@ConfigurationPropertiesBinding
@Component
public static class ColorConverter implements Converter<String, Color>{
}

The @EnableConfigurationProperties annotation made no difference - sounds like that is included automagically by Spring Boot (ref: https://www.baeldung.com/spring-enable-config-properties )