From aadbf1d8a6c0c34112322d2de32be20cda6fd6e7 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 5 May 2016 20:53:12 -0400 Subject: [PATCH] Order ViewResolver's --- .../view/ViewResolverResultHandler.java | 2 ++ .../view/ViewResolverResultHandlerTests.java | 32 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/view/ViewResolverResultHandler.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/view/ViewResolverResultHandler.java index 317e23e2a5..bdf89c54c2 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/view/ViewResolverResultHandler.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/view/ViewResolverResultHandler.java @@ -25,6 +25,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import org.springframework.core.Ordered; +import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.convert.ConversionService; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.util.Assert; @@ -59,6 +60,7 @@ public class ViewResolverResultHandler implements HandlerResultHandler, Ordered Assert.notEmpty(resolvers, "At least one ViewResolver is required."); Assert.notNull(service, "'conversionService' is required."); this.viewResolvers.addAll(resolvers); + AnnotationAwareOrderComparator.sort(this.viewResolvers); this.conversionService = service; } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/view/ViewResolverResultHandlerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/view/ViewResolverResultHandlerTests.java index 6dd15aa541..8d6c2f38e3 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/view/ViewResolverResultHandlerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/view/ViewResolverResultHandlerTests.java @@ -35,6 +35,7 @@ import reactor.core.publisher.Mono; import reactor.core.test.TestSubscriber; import rx.Single; +import org.springframework.core.Ordered; import org.springframework.core.ResolvableType; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.ReactiveStreamsToRxJava1Converter; @@ -190,6 +191,23 @@ public class ViewResolverResultHandlerTests { assertThat(ex.getMessage(), endsWith("neither returned a view name nor a View object"))); } + @Test + public void ordered() throws Exception { + TestViewResolver resolver1 = new TestViewResolver(); + TestViewResolver resolver2 = new TestViewResolver(); + List resolvers = Arrays.asList(resolver1, resolver2); + + resolver1.setOrder(2); + resolver2.setOrder(1); + + ViewResolverResultHandler resultHandler = + new ViewResolverResultHandler(resolvers, this.conversionService); + + assertEquals(Arrays.asList(resolver2, resolver1), resultHandler.getViewResolvers()); + } + + + private TestSubscriber handle(HandlerResultHandler handler, Object value, ResolvableType type) { HandlerResult result = new HandlerResult(new Object(), value, type, this.model); Mono mono = handler.handleResult(this.exchange, result); @@ -210,10 +228,21 @@ public class ViewResolverResultHandlerTests { } - private static class TestViewResolver implements ViewResolver { + private static class TestViewResolver implements ViewResolver, Ordered { private final Map views = new HashMap<>(); + private int order = Ordered.LOWEST_PRECEDENCE; + + + public void setOrder(int order) { + this.order = order; + } + + @Override + public int getOrder() { + return this.order; + } public TestViewResolver addView(TestView view) { this.views.put(view.getName(), view); @@ -225,6 +254,7 @@ public class ViewResolverResultHandlerTests { View view = this.views.get(viewName); return Mono.justOrEmpty(view); } + } public static final class TestView implements View {