Set PathPatternParser in RouterFunctionMapping
See gh-24945
This commit is contained in:
parent
734f4a4706
commit
837dfe285a
|
|
@ -38,6 +38,7 @@ import org.springframework.web.servlet.function.RouterFunction;
|
|||
import org.springframework.web.servlet.function.RouterFunctions;
|
||||
import org.springframework.web.servlet.function.ServerRequest;
|
||||
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
|
||||
/**
|
||||
* {@code HandlerMapping} implementation that supports {@link RouterFunction RouterFunctions}.
|
||||
|
|
@ -76,6 +77,9 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini
|
|||
* {@link RouterFunction} instances available in the application context.
|
||||
*/
|
||||
public RouterFunctionMapping() {
|
||||
// gh-23236 will ensure the configured parser is used to parse patterns
|
||||
// For now this helps to signal to the DispatcherServlet the need to initialize the RequestPath
|
||||
setPatternParser(new PathPatternParser());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ import java.util.stream.Stream;
|
|||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
|
@ -60,6 +61,8 @@ import javax.validation.Valid;
|
|||
import javax.validation.constraints.NotNull;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
||||
import org.springframework.aop.interceptor.SimpleTraceInterceptor;
|
||||
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
||||
|
|
@ -134,11 +137,16 @@ import org.springframework.web.bind.support.WebBindingInitializer;
|
|||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.multipart.support.StringMultipartFileEditor;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.function.RouterFunction;
|
||||
import org.springframework.web.servlet.function.RouterFunctions;
|
||||
import org.springframework.web.servlet.function.ServerResponse;
|
||||
import org.springframework.web.servlet.handler.PathPatternsParameterizedTest;
|
||||
import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
|
@ -163,12 +171,10 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|||
*/
|
||||
public class ServletAnnotationControllerHandlerMethodTests extends AbstractServletHandlerMethodTests {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static Stream<Boolean> pathPatternsArguments() {
|
||||
return Stream.of(true, false);
|
||||
}
|
||||
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void emptyValueMapping(boolean usePathPatterns) throws Exception {
|
||||
initDispatcherServlet(ControllerWithEmptyValueMapping.class, usePathPatterns);
|
||||
|
|
@ -2099,6 +2105,26 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
|
|||
assertThat(response.getContentAsString()).isEqualTo("2010-01-01");
|
||||
}
|
||||
|
||||
@Test
|
||||
void routerFunction() throws ServletException, IOException {
|
||||
GenericWebApplicationContext wac = new GenericWebApplicationContext();
|
||||
wac.registerBean(RouterFunction.class, () ->
|
||||
RouterFunctions.route()
|
||||
.GET("/foo", request -> ServerResponse.ok().body("foo-body"))
|
||||
.build());
|
||||
wac.refresh();
|
||||
|
||||
DispatcherServlet servlet = new DispatcherServlet();
|
||||
servlet.setApplicationContext(wac);
|
||||
servlet.init(new MockServletConfig());
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getContentAsString()).isEqualTo("foo-body");
|
||||
}
|
||||
|
||||
@Controller
|
||||
static class ControllerWithEmptyValueMapping {
|
||||
|
|
|
|||
Loading…
Reference in New Issue