0

I'd like to add Converters via FormattingConversionService, which requires having a @Configuration class extending WebMvcConfigurationSupport:

@Configuration
public class WebAutoConfig extends WebMvcConfigurationSupport {

    @Override
    public FormattingConversionService mvcConversionService() {

        FormattingConversionService fcs = super.mvcConversionService();

        // add Enum converter in order to accept enums
        // case insensitively over Rest:
        fcs.addConverter(
                String.class,
                MyEnum.class,
                new EnumCaseInsensitiveConverter<>( MyEnum.class )
        );

        return fcs;
    }
}

It works just fine when the @Configuration is used directly from the project, but there's requirement to add this logic to our own boot-starter so there would be no need to duplicate code all over the projects.

Problem is, when this @Configuration is migrated to a starter project, then

  • mvcConversionService() is not executed, and
  • RestControllers routing is broken (ie no requests are mapped correctly).

How to approach this? Note using WebMvcConfigurationSupport is not a hard requirement. As seen from the code excerpt, end goal is to configure certain enums to be accepted case-insensitively by the rest controllers.

Edit: it should be added that the auto-config project is correctly set up, as other @Configuration classes in the same package as WebAutoConfig.java are executing. Think the issue has to do with how configuration classes extending WebMvcConfigurationSupport (or WebMvcConfigurerAdapter for that matter) are being handled from auto-configs.

Edit2: only way I've managed to get to work so far is extending the config class from the using project:

import myautoconfproject.autoconfigure.WebAutoConfiguration;

@Configuration
public class WebConfiguration extends WebAutoConfiguration {
}

But that's not really auto-configuration anymore.

laur
  • 498
  • 9
  • 17
  • What is the setup of your starter? There is no real black magic to implementing your own starter, other than conditional loading of it. Are you certain the Configuration would be component scanned by your starter? – Darren Forsythe Sep 27 '17 at 19:07
  • @DarrenForsythe Yes, it's certainly picked up as autoconfig entrypoint includes `@ComponentScan`. Also there are other `@Configuration` classes in the same package that are executing. – laur Sep 27 '17 at 19:31

2 Answers2

0

In order for your configuration to get picked up automatically for projects that include your project as dependency, you need to add the file META-INF/spring.factories to your classpath (in the project where WebAutoConfig lies and add the lines

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
your.package.WebAutoConfig
msp
  • 3,272
  • 7
  • 37
  • 49
  • That's not the problem. `EnableAutoConfiguration` entrypoint is already set in `spring.factories`, pointing to another class in the autoconfig project, that includes `@ComponentScan` pointing to other `@Configuration` classes, including the `WebAutoConfig`. See my comment under the question. The issue, as far as i can tell, appears to be how `WebMvcConfigurerAdapter` is/can be used from autoconfig. – laur Sep 28 '17 at 10:25
0

Apparently I was wrong about the WebMvcConfigurerAdapter - that one is picked up by auto-config:

@Configuration
public class WebAutoConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters( final FormatterRegistry registry ) {

        registry.addConverter(
                String.class,
                MyEnum.class,
                new EnumCaseInsensitiveConverter<>( MyEnum.class )
        );
    }
}
laur
  • 498
  • 9
  • 17