@ -17,17 +17,11 @@
@@ -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;
@@ -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
@@ -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
@@ -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 {
@ -113,45 +117,22 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
@@ -113,45 +117,22 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
}
@Bean
public HandlerAdapter handlerAdapt er( ) {
return new HandlerFunctionAdapt er( ) ;
public PersonController personControll er( ) {
return new PersonControll er( ) ;
}
@Bean
public HandlerMapping handlerMapping ( RouterFunction < ? > routerFunction ,
ApplicationContext applicationContext ) {
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 ;
}
} ) ;
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
@@ -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 ;