2014-10-10 125 views
4

有誰知道如何獲得相同的信息,以及使用哪些路徑,比如在dw應用程序的開始。我的意思是這條線後的輸出:Dropwizard路徑:獲取應用程序中的所有路徑

io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources: 
GET  /path/of/res/test (this.is.the.class.package.info.MyRessource) 
POST  /path/of/res/test2 (this.is.the.class.package.info.MyRessource2) 

我必須檢查是否存在特定路徑。

回答

2

該解決方案對我的作品(DW 0.7.1):

private Multimap<String, String> getEndpoints(Environment environment) 
{ 
    Multimap<String, String> resources = ArrayListMultimap.create(); 
    ResourceConfig jrConfig = environment.jersey().getResourceConfig(); 
    Set<Object> dwSingletons = jrConfig.getSingletons(); 

    for (Object singletons : dwSingletons) {   

     if (singletons.getClass().isAnnotationPresent(Path.class)) {     
      AbstractResource resource = IntrospectionModeller.createResource(singletons.getClass()); 
      AbstractResource superResource = IntrospectionModeller.createResource(singletons.getClass().getSuperclass()); 

      String uriPrefix = getStringWithoutStartingSlash(resource.getPath().getValue()); 

      for (AbstractResourceMethod srm :resource.getResourceMethods()) 
      { 
       String uri = uriPrefix; 
       resources.put(uri,srm.getHttpMethod()); 
       LOG.info("Found http method " +srm.getHttpMethod() + " for the path " + uri + " returning (class) " + srm.getReturnType().getName()); 
      } 


      for (AbstractSubResourceMethod srm :resource.getSubResourceMethods()) 
      {   
       //extended resources methods will be added by hand 
       if(superResource != null){ 
        for (AbstractSubResourceMethod superSrm : superResource.getSubResourceMethods()) 
        { 
         String srmPath = getStringWithoutStartingSlash(srm.getPath().getValue()); 
         String superSrmPath = getStringWithoutStartingSlash(superSrm.getPath().getValue());      

         Class<?> srmClass = srm.getDeclaringResource().getResourceClass(); 
         Class<?> superSrmClass = superSrm.getDeclaringResource().getResourceClass(); 

         //add superclass method if methodName is not equal superMethodName 
         if(srmClass.getSuperclass().equals(superSrmClass) && !srm.getMethod().getName().equals(superSrm.getMethod().getName())){ 
          String uri = uriPrefix + "/" + srmPath + "/" + superSrmPath ;        
          resources.put(uri,superSrm.getHttpMethod()); 
          LOG.info("Found http method " +superSrm.getHttpMethod() + " for the path " + uri + " returning (class) " + superSrm.getReturnType().getName()); 
         } 
        } 
       } 

       String uri = uriPrefix + "/" + getStringWithoutStartingSlash(srm.getPath().getValue()); 
       resources.put(uri,srm.getHttpMethod()); 
       LOG.info("Found http method " +srm.getHttpMethod() + " for the path " + uri + " returning (class) " + srm.getReturnType().getName());   
      }    
     } 
    }   
    return resources; 
    } 

但@PathParam annoations也平淡,例如如果@Path(「/ {id}」)那麼某事。像'.../{id}'將會被使用!

如果您擴展您的資源並且超類也有路徑註釋,那麼此方法將產生的信息甚至超過默認的DW logEndpoints()方法!

FYI:在

import java.util.Set; 
import javax.ws.rs.Path; 
import com.google.common.collect.ArrayListMultimap; 
import com.google.common.collect.Multimap; 
import com.sun.jersey.api.core.ResourceConfig; 
import com.sun.jersey.api.model.AbstractResource; 
import com.sun.jersey.api.model.AbstractResourceMethod; 
import com.sun.jersey.api.model.AbstractSubResourceMethod; 
import com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import io.dropwizard.setup.Environment; 
+0

愛這個..! :) – 2016-04-06 14:07:28

3

你必須自己做。看看logEndpoints method(這是實際記錄此信息 - 用私人方法)。在您的run方法中配置資源後,您應該能夠使用此方法處理environment.jersey().getResourceConfig()中的資源。

喜歡的東西:

final ImmutableList.Builder<Class<?>> builder = ImmutableList.builder(); 
for (Object o : environment.jersey().getResourceConfig().getSingletons()) { 
    if (o.getClass().isAnnotationPresent(Path.class)) { 
    builder.add(o.getClass()); 
    } 
} 
for (Class<?> klass : environment.jersey().getResourceConfig().getClasses()) { 
    if (klass.isAnnotationPresent(Path.class)) { 
    builder.add(klass); 
    } 
} 
final List<String> endpoints = Lists.newArrayList(); 
for (Class<?> klass : builder.build()) { 
    AbstractResource resource = IntrospectionModeller.createResource(klass); 
    endpoints.add(resource.getPath().getValue()); 
} 

注意什麼在主稍稍領先的什麼在Maven的 - 上面的例子說明如何獲得AbstractResource將與0.7.1工作。隨着dropwizard的發展,您必須確保適應您的方法。這個例子也沒有標準化路徑,但我可以根據logEndpoints輕鬆添加。

+0

此代碼不包含任何擴展資源的路徑,也不包含類方法的路徑! – user3280180 2014-10-13 07:10:04

2

使用的進口我用一個簡單的方法爲獲得相同的數據。這裏的所有資源都是球衣資源。

Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(Path.class); 

    Collection<Object> values = beansWithAnnotation.values(); 
    for (Object next : values) { 
      ResourceUtil.getResourceUrls(next); 
    } 


public static List<String> getResourceUrls(Object obj) 
{ 
    Resource resource = Resource.from(obj.getClass()); 
    String uriPrefix = resource.getPath(); 
    List<String> urls = new ArrayList<>(); 
    for (Resource res :resource.getChildResources()) 
    { 
     String uri = uriPrefix + res.getPath(); 
     urls.add(uri); 
    } 
    return urls; 
}