Path Matching

你可以定制与路径匹配和 URL 处理相关的选项。有关各个选项的详细信息,请参阅https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.html[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"/>