Search in sources :

Example 1 with HandlerFunction

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

the class HandlerFunctionAdapter method handle.

@Nullable
@Override
public ModelAndView handle(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Object handler) throws Exception {
    WebAsyncManager asyncManager = getWebAsyncManager(servletRequest, servletResponse);
    ServerRequest serverRequest = getServerRequest(servletRequest);
    ServerResponse serverResponse;
    if (asyncManager.hasConcurrentResult()) {
        serverResponse = handleAsync(asyncManager);
    } else {
        HandlerFunction<?> handlerFunction = (HandlerFunction<?>) handler;
        serverResponse = handlerFunction.handle(serverRequest);
    }
    if (serverResponse != null) {
        return serverResponse.writeTo(servletRequest, servletResponse, new ServerRequestContext(serverRequest));
    } else {
        return null;
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) ServerResponse(org.springframework.web.servlet.function.ServerResponse) HandlerFunction(org.springframework.web.servlet.function.HandlerFunction) ServerRequest(org.springframework.web.servlet.function.ServerRequest) Nullable(org.springframework.lang.Nullable)

Example 2 with HandlerFunction

use of org.springframework.web.servlet.function.HandlerFunction 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 HandlerFunction

use of org.springframework.web.servlet.function.HandlerFunction 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 4 with HandlerFunction

use of org.springframework.web.servlet.function.HandlerFunction 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

HandlerFunction (org.springframework.web.servlet.function.HandlerFunction)4 ServerResponse (org.springframework.web.servlet.function.ServerResponse)4 Collections (java.util.Collections)3 List (java.util.List)3 Optional (java.util.Optional)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Test (org.junit.jupiter.api.Test)3 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)3 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)3 HandlerMapping (org.springframework.web.servlet.HandlerMapping)3 RouterFunction (org.springframework.web.servlet.function.RouterFunction)3 RouterFunctions (org.springframework.web.servlet.function.RouterFunctions)3 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)3 ServletRequestPathUtils (org.springframework.web.util.ServletRequestPathUtils)3 PathPatternParser (org.springframework.web.util.pattern.PathPatternParser)3 Nullable (org.springframework.lang.Nullable)1 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)1 ServerRequest (org.springframework.web.servlet.function.ServerRequest)1