diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java index b72629e5974..c627f0e9996 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,6 +63,9 @@ public class PathMatchConfigurer { @Nullable private PathMatcher pathMatcher; + @Nullable + private PathPatternParser defaultPatternParser; + @Nullable private UrlPathHelper defaultUrlPathHelper; @@ -226,7 +229,7 @@ public class PathMatchConfigurer { } /** - * Return the configure UrlPathHelper instance or a default (shared) instance. + * Return the configure UrlPathHelper or a default, shared instance otherwise. * @since 5.3 */ protected UrlPathHelper getUrlPathHelperOrDefault() { @@ -240,7 +243,7 @@ public class PathMatchConfigurer { } /** - * Return the configure PathMatcher instance or a default (shared) instance. + * Return the configure PathMatcher or a default, shared, instance otherwise. * @since 5.3 */ protected PathMatcher getPathMatcherOrDefault() { @@ -253,4 +256,17 @@ public class PathMatchConfigurer { return this.defaultPathMatcher; } + /** + * Return the configured PathPatternParser or a default, shared, instance otherwise. + * @since 5.3.4 + */ + public PathPatternParser getPatternParserOrDefault() { + if (this.patternParser != null) { + return this.patternParser; + } + if (this.defaultPatternParser == null) { + this.defaultPatternParser = new PathPatternParser(); + } + return this.defaultPatternParser; + } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 17c1c10fc8a..545f513dfb9 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -395,6 +395,17 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv protected void configurePathMatch(PathMatchConfigurer configurer) { } + /** + * Return a global {@link PathPatternParser} instance to use for parsing + * patterns to match to the {@link org.springframework.http.server.RequestPath}. + * The returned instance can be configured using + * {@link #configurePathMatch(PathMatchConfigurer)}. + * @since 5.3.4 + */ + @Bean + public PathPatternParser mvcPatternParser() { + return getPathMatchConfigurer().getPatternParserOrDefault(); + } /** * Return a global {@link UrlPathHelper} instance which is used to resolve diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfigurationTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfigurationTests.java index 0b4e1d188cd..a12fb97cd05 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfigurationTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfigurationTests.java @@ -274,7 +274,6 @@ public class DelegatingWebMvcConfigurationTests { @Test public void configurePathPatternParser() { - PathPatternParser patternParser = new PathPatternParser(); PathMatcher pathMatcher = mock(PathMatcher.class); UrlPathHelper pathHelper = mock(UrlPathHelper.class); @@ -313,7 +312,9 @@ public class DelegatingWebMvcConfigurationTests { webMvcConfig.mvcResourceUrlProvider()); assertThat(annotationsMapping).isNotNull(); - assertThat(annotationsMapping.getPatternParser()).isSameAs(patternParser); + assertThat(annotationsMapping.getPatternParser()) + .isSameAs(patternParser) + .isSameAs(webMvcConfig.mvcPatternParser()); configAssertion.accept(annotationsMapping.getUrlPathHelper(), annotationsMapping.getPathMatcher()); SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) webMvcConfig.viewControllerHandlerMapping( @@ -344,4 +345,5 @@ public class DelegatingWebMvcConfigurationTests { assertThat(webMvcConfig.mvcResourceUrlProvider().getUrlPathHelper()).isSameAs(pathHelper); assertThat(webMvcConfig.mvcResourceUrlProvider().getPathMatcher()).isSameAs(pathMatcher); } + }