From 64c0ec3d5e0fe8a39fee7def55587a19ec5293ed Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 9 May 2017 16:38:11 +0200 Subject: [PATCH] Add test for combining @Controller with RouterFunctions This commit adds a test for combining RouterFunctions with a @Controller. Issue: SPR-15521 --- .../DispatcherHandlerIntegrationTests.java | 75 ++++++++----------- 1 file changed, 33 insertions(+), 42 deletions(-) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DispatcherHandlerIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DispatcherHandlerIntegrationTests.java index 3e8154853ae..381aa2dbfc0 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DispatcherHandlerIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DispatcherHandlerIntegrationTests.java @@ -17,17 +17,11 @@ package org.springframework.web.reactive.function.server; import java.util.List; -import java.util.Locale; -import java.util.Optional; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Stream; import org.junit.Test; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -35,18 +29,19 @@ import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.http.codec.HttpMessageReader; -import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.server.reactive.AbstractHttpHandlerIntegrationTests; import org.springframework.http.server.reactive.HttpHandler; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.DispatcherHandler; import org.springframework.web.reactive.HandlerAdapter; import org.springframework.web.reactive.HandlerMapping; +import org.springframework.web.reactive.config.EnableWebFlux; import org.springframework.web.reactive.config.WebFluxConfigurationSupport; import org.springframework.web.reactive.function.server.support.HandlerFunctionAdapter; import org.springframework.web.reactive.function.server.support.ServerResponseResultHandler; -import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import static org.junit.Assert.*; @@ -56,7 +51,7 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.r /** * Tests the use of {@link HandlerFunction} and {@link RouterFunction} in a - * {@link DispatcherHandler}. + * {@link DispatcherHandler}, combined with {@link Controller}s. * * @author Arjen Poutsma */ @@ -103,6 +98,15 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr assertEquals("Jane", body.get(1).getName()); } + @Test + public void controller() throws Exception { + ResponseEntity result = + this.restTemplate.getForEntity("http://localhost:" + this.port + "/controller", Person.class); + + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals("John", result.getBody().getName()); + } + @Configuration static class TestConfiguration extends WebFluxConfigurationSupport { @@ -113,45 +117,22 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr } @Bean - public HandlerAdapter handlerAdapter() { - return new HandlerFunctionAdapter(); + public PersonController personController() { + return new PersonController(); } @Bean - public HandlerMapping handlerMapping(RouterFunction routerFunction, - ApplicationContext applicationContext) { - - return RouterFunctions.toHandlerMapping(routerFunction, - new HandlerStrategies() { - @Override - public Supplier>> messageReaders() { - return () -> serverCodecConfigurer().getReaders().stream() - .map(reader -> (HttpMessageReader) reader); - } - - @Override - public Supplier>> messageWriters() { - return () -> serverCodecConfigurer().getWriters().stream() - .map(writer -> (HttpMessageWriter) writer); - } - - @Override - public Supplier> viewResolvers() { - return Stream::empty; - } - - @Override - public Supplier>> localeResolver() { - return () -> DefaultHandlerStrategiesBuilder.DEFAULT_LOCALE_RESOLVER; - } - }); + public HandlerAdapter handlerAdapter() { + return new HandlerFunctionAdapter(); } @Bean - public RouterFunction routerFunction() { + public HandlerMapping handlerMapping() { + PersonHandler personHandler = personHandler(); - return route(RequestPredicates.GET("/mono"), personHandler::mono) - .and(route(RequestPredicates.GET("/flux"), personHandler::flux)); + return RouterFunctions.toHandlerMapping( + route(RequestPredicates.GET("/mono"), personHandler::mono) + .and(route(RequestPredicates.GET("/flux"), personHandler::flux))); } @Bean @@ -177,6 +158,16 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr } + @Controller + private static class PersonController { + + @RequestMapping("/controller") + @ResponseBody + public Mono controller() { + return Mono.just(new Person("John")); + } + } + private static class Person { private String name;