提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.framework.handler; |
X |
2 |
|
|
3 |
import cn.hutool.core.util.ReUtil; |
|
4 |
import com.dl.common.utils.spring.SpringUtils; |
|
5 |
import lombok.Data; |
|
6 |
import org.springframework.beans.factory.InitializingBean; |
|
7 |
import org.springframework.stereotype.Component; |
|
8 |
import org.springframework.web.method.HandlerMethod; |
|
9 |
import org.springframework.web.servlet.mvc.method.RequestMappingInfo; |
|
10 |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
|
11 |
|
|
12 |
import java.util.*; |
|
13 |
import java.util.regex.Pattern; |
|
14 |
|
|
15 |
/** |
|
16 |
* 获取所有Url配置 |
|
17 |
* |
|
18 |
* @author Lion Li |
|
19 |
*/ |
|
20 |
@Data |
|
21 |
@Component |
|
22 |
public class AllUrlHandler implements InitializingBean { |
|
23 |
|
|
24 |
private static final Pattern PATTERN = Pattern.compile("\\{(.*?)\\}"); |
|
25 |
|
|
26 |
private List<String> urls = new ArrayList<>(); |
|
27 |
|
|
28 |
@Override |
|
29 |
public void afterPropertiesSet() { |
|
30 |
Set<String> set = new HashSet<>(); |
|
31 |
RequestMappingHandlerMapping mapping = SpringUtils.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class); |
|
32 |
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods(); |
|
33 |
map.keySet().forEach(info -> { |
|
34 |
// 获取注解上边的 path 替代 path variable 为 * |
|
35 |
Objects.requireNonNull(info.getPathPatternsCondition().getPatterns()) |
|
36 |
.forEach(url -> set.add(ReUtil.replaceAll(url.getPatternString(), PATTERN, "*"))); |
|
37 |
}); |
|
38 |
urls.addAll(set); |
|
39 |
} |
|
40 |
|
|
41 |
} |