Dropwizard路径:获取应用中的所有路径

时间:2014-10-10 10:47:53

标签: java dropwizard

有没有人知道如何获得相同的信息,关于使用什么路径,比如dw应用程序的开始。我的意思是这一行之后的输出:

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)

我必须检查是否存在特定路径。

3 个答案:

答案 0 :(得分:3)

你必须自己做这件事。看看logEndpoints method(这是实际记录此信息的内容 - 使用私有方法)。在使用environment.jersey().getResourceConfig()方法配置资源后,您应该能够调整此方法来处理run中的资源。

类似的东西:

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

请注意,master中的内容略高于Maven中的内容 - 上面的示例显示了如何获得适用于0.7.1的AbstractResource。随着dropwizard的发展,你必须确保调整你的方法。此示例也没有对路径进行规范化,但我可以根据logEndpoints轻松添加该路径。

答案 1 :(得分: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()方法更多!

仅供参考:类

中使用的导入
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;

答案 2 :(得分:2)

我使用更简单的方法来获取相同的数据。这里的所有资源都是球衣资源。

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String) As Long

Function PrintAttachement() 
ShellExecute 0, "print", "\\s1016d\attachments\40297827.pdf", "", ""

End Function