Search in sources :

Example 6 with RouterFunction

use of org.springframework.web.servlet.function.RouterFunction in project spring-framework-debug by Joker-5.

the class RouterFunctionMapping method initRouterFunction.

/**
 * Detect a all {@linkplain RouterFunction router functions} in the
 * current application context.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void initRouterFunction() {
    ApplicationContext applicationContext = obtainApplicationContext();
    Map<String, RouterFunction> beans = (this.detectHandlerFunctionsInAncestorContexts ? BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RouterFunction.class) : applicationContext.getBeansOfType(RouterFunction.class));
    List<RouterFunction> routerFunctions = new ArrayList<>(beans.values());
    if (!CollectionUtils.isEmpty(routerFunctions) && logger.isInfoEnabled()) {
        routerFunctions.forEach(routerFunction -> logger.info("Mapped " + routerFunction));
    }
    this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ArrayList(java.util.ArrayList) RouterFunction(org.springframework.web.servlet.function.RouterFunction)

Example 7 with RouterFunction

use of org.springframework.web.servlet.function.RouterFunction in project spring-framework by spring-projects.

the class RouterFunctionMappingTests method changeParser.

@Test
void changeParser() throws Exception {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route().GET("/foo", handlerFunction).POST("/bar", handlerFunction).build();
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageConverters(this.messageConverters);
    PathPatternParser patternParser = new PathPatternParser();
    patternParser.setCaseSensitive(false);
    mapping.setPatternParser(patternParser);
    mapping.afterPropertiesSet();
    MockHttpServletRequest request = createTestRequest("/FOO");
    HandlerExecutionChain result = mapping.getHandler(request);
    assertThat(result).isNotNull();
    assertThat(result.getHandler()).isSameAs(handlerFunction);
}
Also used : ServerResponse(org.springframework.web.servlet.function.ServerResponse) HandlerFunction(org.springframework.web.servlet.function.HandlerFunction) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RouterFunctions(org.springframework.web.servlet.function.RouterFunctions) ServletRequestPathUtils(org.springframework.web.util.ServletRequestPathUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerResponse(org.springframework.web.servlet.function.ServerResponse) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.jupiter.api.Test) RouterFunction(org.springframework.web.servlet.function.RouterFunction) List(java.util.List) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Optional(java.util.Optional) Collections(java.util.Collections) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Test(org.junit.jupiter.api.Test)

Example 8 with RouterFunction

use of org.springframework.web.servlet.function.RouterFunction in project spring-framework by spring-projects.

the class RouterFunctionMappingTests method mappedRequestShouldHoldAttributes.

@Test
void mappedRequestShouldHoldAttributes() throws Exception {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route().GET("/match", handlerFunction).build();
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageConverters(this.messageConverters);
    MockHttpServletRequest request = createTestRequest("/match");
    HandlerExecutionChain result = mapping.getHandler(request);
    assertThat(result).isNotNull();
    assertThat(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)).isEqualTo("/match");
    assertThat(request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(handlerFunction);
}
Also used : ServerResponse(org.springframework.web.servlet.function.ServerResponse) HandlerFunction(org.springframework.web.servlet.function.HandlerFunction) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RouterFunctions(org.springframework.web.servlet.function.RouterFunctions) ServletRequestPathUtils(org.springframework.web.util.ServletRequestPathUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerResponse(org.springframework.web.servlet.function.ServerResponse) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.jupiter.api.Test) RouterFunction(org.springframework.web.servlet.function.RouterFunction) List(java.util.List) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Optional(java.util.Optional) Collections(java.util.Collections) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Aggregations

RouterFunction (org.springframework.web.servlet.function.RouterFunction)8 Collections (java.util.Collections)4 List (java.util.List)4 Optional (java.util.Optional)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Test (org.junit.jupiter.api.Test)4 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)4 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)4 HandlerMapping (org.springframework.web.servlet.HandlerMapping)4 HandlerFunction (org.springframework.web.servlet.function.HandlerFunction)4 RouterFunctions (org.springframework.web.servlet.function.RouterFunctions)4 ServerResponse (org.springframework.web.servlet.function.ServerResponse)4 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)4 ServletRequestPathUtils (org.springframework.web.util.ServletRequestPathUtils)4 PathPatternParser (org.springframework.web.util.pattern.PathPatternParser)4 ArrayList (java.util.ArrayList)3 ApplicationContext (org.springframework.context.ApplicationContext)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AbstractRouterFunctionVisitor (org.springdoc.core.fn.AbstractRouterFunctionVisitor)1