路徑匹配
您可以自訂與路徑匹配和 URL 處理相關的選項。有關各個選項的詳細資訊,請參閱 PathMatchConfigurer
javadoc。
以下範例展示如何自訂路徑匹配
-
Java
-
Kotlin
-
Xml
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.addPathPrefix("/api", HandlerTypePredicate.forAnnotation(RestController.class));
}
private PathPatternParser patternParser() {
PathPatternParser pathPatternParser = new PathPatternParser();
// ...
return pathPatternParser;
}
}
@Configuration
class WebConfiguration : WebMvcConfigurer {
override fun configurePathMatch(configurer: PathMatchConfigurer) {
configurer.addPathPrefix("/api", HandlerTypePredicate.forAnnotation(RestController::class.java))
}
fun patternParser(): PathPatternParser {
val pathPatternParser = PathPatternParser()
//...
return pathPatternParser
}
}
<mvc:annotation-driven>
<mvc:path-matching
path-helper="pathHelper"
path-matcher="pathMatcher"/>
</mvc:annotation-driven>
<bean id="pathHelper" class="org.example.app.MyPathHelper"/>
<bean id="pathMatcher" class="org.example.app.MyPathMatcher"/>