AutoPrefixUrlMapping.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {
private String apiPackagePath;
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMappingInfo mappingInfo = super.getMappingForMethod(method, handlerType);
if(mappingInfo != null){
String prefix = this.getPrefix(handlerType);
return RequestMappingInfo.paths(prefix).build().combine(mappingInfo);
}
return mappingInfo;
}
private String getPrefix(Class<?> handlerType){
String packageName = handlerType.getPackage().getName();
String dotPath = packageName.replaceAll(this.apiPackagePath,"");
return dotPath.replace(".", "/");
}
}AutoPrefixConfiguration.java
1
2
3
4
5
6
7
8
9
public class AutoPrefixConfiguration implements WebMvcRegistrations {
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new AutoPrefixUrlMapping();
}
}
- application.yml
1 | api-package: com.example.demo |
- DemoController
1 |
|
- 目录结构