001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.config.annotation; 018 019import org.springframework.util.PathMatcher; 020import org.springframework.web.util.UrlPathHelper; 021 022/** 023 * Helps with configuring HandlerMappings path matching options such as trailing 024 * slash match, suffix registration, path matcher and path helper. 025 * 026 * <p>Configured path matcher and path helper instances are shared for: 027 * <ul> 028 * <li>RequestMappings</li> 029 * <li>ViewControllerMappings</li> 030 * <li>ResourcesMappings</li> 031 * </ul> 032 * 033 * @author Brian Clozel 034 * @since 4.0.3 035 * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 036 * @see org.springframework.web.servlet.handler.SimpleUrlHandlerMapping 037 */ 038public class PathMatchConfigurer { 039 040 private Boolean suffixPatternMatch; 041 042 private Boolean trailingSlashMatch; 043 044 private Boolean registeredSuffixPatternMatch; 045 046 private UrlPathHelper urlPathHelper; 047 048 private PathMatcher pathMatcher; 049 050 051 /** 052 * Whether to use suffix pattern match (".*") when matching patterns to 053 * requests. If enabled a method mapped to "/users" also matches to "/users.*". 054 * <p>By default this is set to {@code true}. 055 * @see #registeredSuffixPatternMatch 056 */ 057 public PathMatchConfigurer setUseSuffixPatternMatch(Boolean suffixPatternMatch) { 058 this.suffixPatternMatch = suffixPatternMatch; 059 return this; 060 } 061 062 /** 063 * Whether to match to URLs irrespective of the presence of a trailing slash. 064 * If enabled a method mapped to "/users" also matches to "/users/". 065 * <p>The default value is {@code true}. 066 */ 067 public PathMatchConfigurer setUseTrailingSlashMatch(Boolean trailingSlashMatch) { 068 this.trailingSlashMatch = trailingSlashMatch; 069 return this; 070 } 071 072 /** 073 * Whether suffix pattern matching should work only against path extensions 074 * explicitly registered when you 075 * {@link WebMvcConfigurer#configureContentNegotiation configure content 076 * negotiation}. This is generally recommended to reduce ambiguity and to 077 * avoid issues such as when a "." appears in the path for other reasons. 078 * <p>By default this is set to "false". 079 * @see WebMvcConfigurer#configureContentNegotiation 080 */ 081 public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(Boolean registeredSuffixPatternMatch) { 082 this.registeredSuffixPatternMatch = registeredSuffixPatternMatch; 083 return this; 084 } 085 086 /** 087 * Set the UrlPathHelper to use for resolution of lookup paths. 088 * <p>Use this to override the default UrlPathHelper with a custom subclass, 089 * or to share common UrlPathHelper settings across multiple HandlerMappings 090 * and MethodNameResolvers. 091 */ 092 public PathMatchConfigurer setUrlPathHelper(UrlPathHelper urlPathHelper) { 093 this.urlPathHelper = urlPathHelper; 094 return this; 095 } 096 097 /** 098 * Set the PathMatcher implementation to use for matching URL paths 099 * against registered URL patterns. Default is AntPathMatcher. 100 * @see org.springframework.util.AntPathMatcher 101 */ 102 public PathMatchConfigurer setPathMatcher(PathMatcher pathMatcher) { 103 this.pathMatcher = pathMatcher; 104 return this; 105 } 106 107 108 public Boolean isUseSuffixPatternMatch() { 109 return this.suffixPatternMatch; 110 } 111 112 public Boolean isUseTrailingSlashMatch() { 113 return this.trailingSlashMatch; 114 } 115 116 public Boolean isUseRegisteredSuffixPatternMatch() { 117 return this.registeredSuffixPatternMatch; 118 } 119 120 public UrlPathHelper getUrlPathHelper() { 121 return this.urlPathHelper; 122 } 123 124 public PathMatcher getPathMatcher() { 125 return this.pathMatcher; 126 } 127 128}