我可以在Spring中使用GET方法获取所有requestMapping URL吗? [英] Can I get all of requestMapping URL with GET method in the Spring?
问题描述
我想动态地创建一个sitemap.xml文件。
如果我需要在控制器中获得所有的url地址,
我该如何解决这类问题?
我想要的做的是用spring生成sitemap.xml。
sitemap.xml具有搜索引擎在我的网站
上爬行的所有url,这就是为什么我需要这个解决方案。
以下代码从类型中提取所有 RequestMappingInfo
和方法级别 @RequestMapping
@Controller
类中的注释。
// context = ApplicationContext
Map< String,RequestMappingHandlerMapping> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context,
RequestMappingHandlerMapping.class,true,false);
if(!matchingBeans.isEmpty()){
ArrayList< HandlerMapping> handlerMappings = new ArrayList< HandlerMapping>(matchingBeans.values());
AnnotationAwareOrderComparator.sort(handlerMappings);
RequestMappingHandlerMapping mappings = matchingBeans.get(requestMappingHandlerMapping);
Map< RequestMappingInfo,HandlerMethod> handlerMethods = mappings.getHandlerMethods(); (RequestMappingInfo requestMappingInfo:handlerMethods.keySet()){
RequestMethodsRequestCondition methods = requestMappingInfo.getMethodsCondition();
。
//使用
获得所有的requestMappingInfos // 1)默认的request-method(这是none)
// 2)或method = GET
if .getMethods()。isEmpty()|| methods.getMethods()。contains(RequestMethod.GET)){
System.out.println(requestMappingInfo.getPatternsCondition()。getPatterns()+ - >产生 +
requestMappingInfo.getProducesCondition());
}
}
}
您可能需要过滤掉映射错误页面。
RequestMappingInfo
对象包含您通过 @RequestMapping
注释定义的所有相关映射信息,例如:
-
RequestMappingInfo.getMethods()
- >@RequestMapping方法= RequestMethod.GET)
-
RequestMappingInfo.getPatternsCondition()。getPatterns()
- >@RequestMapping(value =/ foo)
- 等等。欲了解更多详情,请参阅
RequestMappingInfo
为了进一步抓住例如。 ViewController 配置,您需要过滤 SimpleUrlHandlerMapping
类型:
Map< String,SimpleUrlHandlerMapping> matchingUrlHandlerMappingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context,
SimpleUrlHandlerMapping.class,true,false);
SimpleUrlHandlerMapping mappings = matchingUrlHandlerMappingBeans.get(viewControllerHandlerMapping);
System.out.println(mappings.getUrlMap());
I want to make a sitemap.xml file dynamically. If then I need to get all of url address in the controller, How can I resolve this kinds of thing?
All I want to do is to generate sitemap.xml with spring.
The sitemap.xml have all the url that a search engine should crawl on my site and that's why I need this solution.
Following code extracts all RequestMappingInfo
instances from type and method-level @RequestMapping
annotations in @Controller
classes.
// context = ApplicationContext
Map<String, RequestMappingHandlerMapping> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context,
RequestMappingHandlerMapping.class, true, false);
if (!matchingBeans.isEmpty()) {
ArrayList<HandlerMapping> handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());
AnnotationAwareOrderComparator.sort(handlerMappings);
RequestMappingHandlerMapping mappings = matchingBeans.get("requestMappingHandlerMapping");
Map<RequestMappingInfo, HandlerMethod> handlerMethods = mappings.getHandlerMethods();
for (RequestMappingInfo requestMappingInfo : handlerMethods.keySet()) {
RequestMethodsRequestCondition methods = requestMappingInfo.getMethodsCondition();
// Get all requestMappingInfos with
// 1) default request-method (which is none)
// 2) or method=GET
if (methods.getMethods().isEmpty() || methods.getMethods().contains(RequestMethod.GET)) {
System.out.println(requestMappingInfo.getPatternsCondition().getPatterns() + " -> produces " +
requestMappingInfo.getProducesCondition());
}
}
}
You probably need to filter out mappings for the error-pages.
The RequestMappingInfo
object contains all the relevant mapping information that you define over @RequestMapping
annotations such as:
RequestMappingInfo.getMethods()
->@RequestMapping(method=RequestMethod.GET)
RequestMappingInfo.getPatternsCondition().getPatterns()
->@RequestMapping(value = "/foo")
- etc. for more details see
RequestMappingInfo
To further catch eg. ViewController configurations as well, you need to filter for the SimpleUrlHandlerMapping
type:
Map<String, SimpleUrlHandlerMapping> matchingUrlHandlerMappingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context,
SimpleUrlHandlerMapping.class, true, false);
SimpleUrlHandlerMapping mappings = matchingUrlHandlerMappingBeans.get("viewControllerHandlerMapping");
System.out.println(mappings.getUrlMap());
这篇关于我可以在Spring中使用GET方法获取所有requestMapping URL吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!