SpringUtils 工具类
方便自己使用,mark一下
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
@Component
public class SpringUtils extends SpringUtil {
/**
* 获取当前模块所有对外的URL
* @return
*/
public static TreeSet<String> getAllUrl(){
RequestMappingHandlerMapping mapping = getBean("requestMappingHandlerMapping",RequestMappingHandlerMapping.class);
// 拿到Handler适配器中的全部方法
Map<RequestMappingInfo, HandlerMethod> methodMap = mapping.getHandlerMethods();
TreeSet<String> urlList = new TreeSet<>();
for (RequestMappingInfo info : methodMap.keySet()){
Set<String> urlSet = info.getPatternsCondition().getPatterns();
// 获取全部请求方式
Set<RequestMethod> Methods = info.getMethodsCondition().getMethods();
for (String url : urlSet){
// 加上自己的域名和端口号,就可以直接调用
urlList.add(url);
}
}
urlList.comparator();
return urlList;
}
}