注释类型 EnableWebFlux
@Retention(RUNTIME) @Target(TYPE) @Documented @Import(DelegatingWebFluxConfiguration.class) public @interface EnableWebFlux
Adding this annotation to an@Configuration
class imports the Spring WebFlux configuration fromWebFluxConfigurationSupport
that enables use of annotated controllers and functional endpoints.For example:
@Configuration @EnableWebFlux @ComponentScan public class MyConfiguration { }
To customize the imported configuration, implement
WebFluxConfigurer
and one or more of its methods:@Configuration @EnableWebFlux @ComponentScan public class MyConfiguration implements WebFluxConfigurer { @Autowired private ObjectMapper objectMapper; @Override public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) { configurer.defaultCodecs().jackson2JsonEncoder( new Jackson2JsonEncoder(objectMapper) ); configurer.defaultCodecs().jackson2JsonDecoder( new Jackson2JsonDecoder(objectMapper) ); } // ... }
Only one
@Configuration
class should have the@EnableWebFlux
annotation in order to import the Spring WebFlux configuration. There can however be multiple@Configuration
classes that implementWebFluxConfigurer
that customize the provided configuration.If
WebFluxConfigurer
does not expose some setting that needs to be configured, consider switching to an advanced mode by removing the@EnableWebFlux
annotation and extending directly fromWebFluxConfigurationSupport
orDelegatingWebFluxConfiguration
-- the latter allows detecting and delegating to one or moreWebFluxConfigurer
configuration classes.- 从以下版本开始:
- 5.0
- 作者:
- Brian Clozel, Rossen Stoyanchev
- 另请参阅:
WebFluxConfigurer
,WebFluxConfigurationSupport
,DelegatingWebFluxConfiguration