[英]How to use WebMvcConfigurationSupport from own auto-configuration
I'd like to add Converter
s via FormattingConversionService
, which requires having a @Configuration class extending WebMvcConfigurationSupport
: 我想通过
FormattingConversionService
添加Converter
,它需要具有扩展WebMvcConfigurationSupport
的@Configuration类:
@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. 当直接从项目中使用@Configuration时,它工作得很好,但是需要将此逻辑添加到我们自己的
boot-starter
这样就无需在整个项目中重复代码。
Problem is, when this @Configuration is migrated to a starter project, then 问题是,当此@Configuration迁移到入门项目时,
mvcConversionService()
is not executed, and mvcConversionService()
未执行,并且 How to approach this? 如何处理呢? Note using
WebMvcConfigurationSupport
is not a hard requirement. 请注意,使用
WebMvcConfigurationSupport
并不是硬性要求。 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. 编辑 :应添加正确的自动配置项目,因为与
WebAutoConfig.java
相同的程序包中的其他@Configuration
类正在执行。 Think the issue has to do with how configuration classes extending WebMvcConfigurationSupport
(or WebMvcConfigurerAdapter
for that matter) are being handled from auto-configs. 认为问题与如何从自动配置中处理扩展
WebMvcConfigurationSupport
(或WebMvcConfigurerAdapter
)的配置类有关。
Edit2 : only way I've managed to get to work so far is extending the config class from the using project: Edit2 :到目前为止,我设法工作的唯一方法是从使用项目扩展config类:
import myautoconfproject.autoconfigure.WebAutoConfiguration;
@Configuration
public class WebConfiguration extends WebAutoConfiguration {
}
But that's not really auto-configuration anymore. 但这不再是真正的自动配置。
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 为了使包含项目作为依赖项的项目的配置自动被获取,您需要将文件
META-INF/spring.factories
到类路径(在WebAutoConfig
所在的项目中,并添加以下行)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
your.package.WebAutoConfig
Apparently I was wrong about the WebMvcConfigurerAdapter
- that one is picked up by auto-config: 显然我在
WebMvcConfigurerAdapter
错了-自动配置选择了一个:
@Configuration
public class WebAutoConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters( final FormatterRegistry registry ) {
registry.addConverter(
String.class,
MyEnum.class,
new EnumCaseInsensitiveConverter<>( MyEnum.class )
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.