Change codec auto-configuration in HandlerStrategies

Since SPR-15415 and SPR-15435, the HandlerStrategies and its companion
builder are now using the new CodecConfigurer infrastructure to
configure codecs in a cross-cutting way.

This commit looks for a custom instance of `HandlerStrategies.Builder`
provided by the user, or create a default one if none was found.

Fixes gh-8897
pull/5662/merge
Brian Clozel 8 years ago
parent 50331102b7
commit 23360d11a2

@ -31,8 +31,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.web.reactive.DispatcherHandler; import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.function.server.HandlerStrategies; import org.springframework.web.reactive.function.server.HandlerStrategies;
@ -72,8 +70,7 @@ public class HttpHandlerAutoConfiguration {
@Bean @Bean
public HttpHandler httpHandler() { public HttpHandler httpHandler() {
return WebHttpHandlerBuilder.applicationContext(this.applicationContext) return WebHttpHandlerBuilder.applicationContext(this.applicationContext).build();
.build();
} }
} }
@ -86,45 +83,36 @@ public class HttpHandlerAutoConfiguration {
private final WebSessionManager webSessionManager; private final WebSessionManager webSessionManager;
private final List<HttpMessageReader<?>> messageReaders; private HandlerStrategies.Builder handlerStrategiesBuilder;
private final List<HttpMessageWriter<?>> messageWriters;
private final List<ViewResolver> viewResolvers; private final List<ViewResolver> viewResolvers;
public FunctionalConfig(ObjectProvider<List<WebFilter>> webFilters, public FunctionalConfig(ObjectProvider<List<WebFilter>> webFilters,
ObjectProvider<WebSessionManager> webSessionManager, ObjectProvider<WebSessionManager> webSessionManager,
ObjectProvider<List<HttpMessageReader<?>>> messageReaders, ObjectProvider<HandlerStrategies.Builder> handlerStrategiesBuilder,
ObjectProvider<List<HttpMessageWriter<?>>> messageWriters,
ObjectProvider<List<ViewResolver>> viewResolvers) { ObjectProvider<List<ViewResolver>> viewResolvers) {
this.webFilters = webFilters.getIfAvailable(); this.webFilters = webFilters.getIfAvailable();
if (this.webFilters != null) { if (this.webFilters != null) {
AnnotationAwareOrderComparator.sort(this.webFilters); AnnotationAwareOrderComparator.sort(this.webFilters);
} }
this.webSessionManager = webSessionManager.getIfAvailable(); this.webSessionManager = webSessionManager.getIfAvailable();
this.messageReaders = messageReaders.getIfAvailable(); this.handlerStrategiesBuilder = handlerStrategiesBuilder.getIfAvailable();
this.messageWriters = messageWriters.getIfAvailable();
this.viewResolvers = viewResolvers.getIfAvailable(); this.viewResolvers = viewResolvers.getIfAvailable();
} }
@Bean @Bean
public <T extends ServerResponse> HttpHandler httpHandler( public <T extends ServerResponse> HttpHandler httpHandler(List<RouterFunction<T>> routerFunctions) {
List<RouterFunction<T>> routerFunctions) {
routerFunctions.sort(new AnnotationAwareOrderComparator()); routerFunctions.sort(new AnnotationAwareOrderComparator());
RouterFunction<T> routerFunction = routerFunctions.stream() RouterFunction<T> routerFunction = routerFunctions.stream()
.reduce(RouterFunction::and).get(); .reduce(RouterFunction::and).get();
HandlerStrategies.Builder strategiesBuilder = HandlerStrategies.builder(); if (this.handlerStrategiesBuilder == null) {
if (this.messageReaders != null) { this.handlerStrategiesBuilder = HandlerStrategies.builder();
this.messageReaders.forEach(strategiesBuilder::customMessageReader);
}
if (this.messageWriters != null) {
this.messageWriters.forEach(strategiesBuilder::customMessageWriter);
} }
if (this.viewResolvers != null) { if (this.viewResolvers != null) {
this.viewResolvers.forEach(strategiesBuilder::viewResolver); this.viewResolvers.forEach(this.handlerStrategiesBuilder::viewResolver);
} }
WebHandler webHandler = RouterFunctions.toHttpHandler(routerFunction, WebHandler webHandler = RouterFunctions.toHttpHandler(routerFunction,
strategiesBuilder.build()); this.handlerStrategiesBuilder.build());
WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.webHandler(webHandler) WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.webHandler(webHandler)
.sessionManager(this.webSessionManager); .sessionManager(this.webSessionManager);
builder.filters(this.webFilters); builder.filters(this.webFilters);

Loading…
Cancel
Save