Search in sources :

Example 1 with RouterFunction

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

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());
    this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null);
    logRouterFunctions(routerFunctions);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ArrayList(java.util.ArrayList) RouterFunction(org.springframework.web.servlet.function.RouterFunction)

Example 2 with RouterFunction

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

the class RouterFunctionMappingTests method normal.

@Test
void normal() throws Exception {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    RouterFunction<ServerResponse> routerFunction = request -> Optional.of(handlerFunction);
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageConverters(this.messageConverters);
    MockHttpServletRequest request = createTestRequest("/match");
    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) Test(org.junit.jupiter.api.Test)

Example 3 with RouterFunction

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

the class RouterFunctionMappingTests method noMatch.

@Test
void noMatch() throws Exception {
    RouterFunction<ServerResponse> routerFunction = request -> Optional.empty();
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageConverters(this.messageConverters);
    MockHttpServletRequest request = createTestRequest("/match");
    HandlerExecutionChain result = mapping.getHandler(request);
    assertThat(result).isNull();
}
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)

Example 4 with RouterFunction

use of org.springframework.web.servlet.function.RouterFunction in project springdoc-openapi by springdoc.

the class RouterFunctionWebMvcProvider method getRouterFunctionPaths.

/**
 * Gets web mvc router function paths.
 *
 * @return the web mvc router function paths
 */
public Optional<Map<String, AbstractRouterFunctionVisitor>> getRouterFunctionPaths() {
    Map<String, RouterFunction> routerBeans = applicationContext.getBeansOfType(RouterFunction.class);
    if (CollectionUtils.isEmpty(routerBeans))
        return Optional.empty();
    Map<String, AbstractRouterFunctionVisitor> routerFunctionVisitorMap = new HashMap<>();
    for (Map.Entry<String, RouterFunction> entry : routerBeans.entrySet()) {
        RouterFunction routerFunction = entry.getValue();
        RouterFunctionVisitor routerFunctionVisitor = new RouterFunctionVisitor();
        routerFunction.accept(routerFunctionVisitor);
        routerFunctionVisitorMap.put(entry.getKey(), routerFunctionVisitor);
    }
    return Optional.of(routerFunctionVisitorMap);
}
Also used : HashMap(java.util.HashMap) AbstractRouterFunctionVisitor(org.springdoc.core.fn.AbstractRouterFunctionVisitor) RouterFunction(org.springframework.web.servlet.function.RouterFunction) AbstractRouterFunctionVisitor(org.springdoc.core.fn.AbstractRouterFunctionVisitor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 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)

Aggregations

RouterFunction (org.springframework.web.servlet.function.RouterFunction)6 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)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AbstractRouterFunctionVisitor (org.springdoc.core.fn.AbstractRouterFunctionVisitor)1 ApplicationContext (org.springframework.context.ApplicationContext)1