Skip to content

Instantly share code, notes, and snippets.

@strongant
Created February 28, 2023 14:34
Show Gist options
  • Save strongant/ae9df4c5769ed9c789401f089f2b9ef6 to your computer and use it in GitHub Desktop.
Save strongant/ae9df4c5769ed9c789401f089f2b9ef6 to your computer and use it in GitHub Desktop.
获取到springboot中所有的请求url
@Component
public class UrlCollector implements ApplicationListener<ContextRefreshedEvent> {
private final List<String> urls = new ArrayList<>();
@Autowired
public UrlCollector(RequestMappingHandlerMapping mapping) {
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
map.forEach((info, method) -> {
PatternsRequestCondition condition = info.getPatternsCondition();
Set<String> patterns = condition.getPatterns();
urls.addAll(patterns);
});
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// 在启动时打印所有HTTP接口
System.out.println("All HTTP interfaces:");
urls.forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment