2016-03-19 5 views
1

sitemap.xmlファイルを動的に作成したいと考えています。 コントローラにURLアドレスをすべて取得する必要がある場合は、 どうすればこの問題を解決できますか?SpringでGETメソッドを使用してrequestMapping URLをすべて取得できますか?

私がしたいことは、春にsitemap.xmlを生成することです。

sitemap.xmlには、検索エンジンが自分のサイト でクロールする必要があるすべてのURLが含まれているため、この解決策が必要です。

答えて

2

次のコードはすべて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(); 

    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()); 
     } 
    } 
} 

おそらくエラーページのマッピングを除外する必要があります。 RequestMappingInfoオブジェクトは、次のような@RequestMapping以上のアノテーションを定義することを、関連するすべてのマッピング情報が含まれています: - >@RequestMapping(method=RequestMethod.GET)

  • RequestMappingInfo.getPatternsCondition().getPatterns() -

    • RequestMappingInfo.getMethods()詳細を見る>@RequestMapping(value = "/foo")
    • などにRequestMappingInfo

    さらにcアーチ。 ViewController構成としても、あなたはSimpleUrlHandlerMappingタイプのためにフィルタリングする必要があります。

    Map<String, SimpleUrlHandlerMapping> matchingUrlHandlerMappingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, 
          SimpleUrlHandlerMapping.class, true, false); 
    SimpleUrlHandlerMapping mappings = matchingUrlHandlerMappingBeans.get("viewControllerHandlerMapping"); 
    System.out.println(mappings.getUrlMap()); 
    
  • +0

    はどうもありがとうございました:) – Richard

    +0

    私は助けることができるうれしいです! – fateddy

    関連する問題