Add test for combining @Controller with RouterFunctions
This commit adds a test for combining RouterFunctions with a @Controller. Issue: SPR-15521
This commit is contained in:
parent
9273197f11
commit
64c0ec3d5e
|
@ -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<Person> 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 {
|
||||
|
@ -112,46 +116,23 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
|
|||
return new PersonHandler();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersonController personController() {
|
||||
return new PersonController();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HandlerAdapter handlerAdapter() {
|
||||
return new HandlerFunctionAdapter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HandlerMapping handlerMapping(RouterFunction<?> routerFunction,
|
||||
ApplicationContext applicationContext) {
|
||||
public HandlerMapping handlerMapping() {
|
||||
|
||||
return RouterFunctions.toHandlerMapping(routerFunction,
|
||||
new HandlerStrategies() {
|
||||
@Override
|
||||
public Supplier<Stream<HttpMessageReader<?>>> messageReaders() {
|
||||
return () -> serverCodecConfigurer().getReaders().stream()
|
||||
.map(reader -> (HttpMessageReader<?>) reader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<Stream<HttpMessageWriter<?>>> messageWriters() {
|
||||
return () -> serverCodecConfigurer().getWriters().stream()
|
||||
.map(writer -> (HttpMessageWriter<?>) writer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<Stream<ViewResolver>> viewResolvers() {
|
||||
return Stream::empty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<Function<ServerRequest, Optional<Locale>>> localeResolver() {
|
||||
return () -> DefaultHandlerStrategiesBuilder.DEFAULT_LOCALE_RESOLVER;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouterFunction<?> routerFunction() {
|
||||
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<Person> controller() {
|
||||
return Mono.just(new Person("John"));
|
||||
}
|
||||
}
|
||||
|
||||
private static class Person {
|
||||
|
||||
private String name;
|
||||
|
|
Loading…
Reference in New Issue