Java Dropwizard路径:获取应用程序中的所有路径
是否有人知道如何获得相同的信息,关于所使用的路径,比如在dw应用程序的开始时。我指的是这行之后的输出:Java Dropwizard路径:获取应用程序中的所有路径,java,dropwizard,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/r
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)
我必须检查特定路径是否存在。您必须自己执行此操作。请看一看(实际上是用私有方法记录此信息的)。在
run
方法中配置资源后,您应该能够调整此方法来处理环境.jersey().getResourceConfig()
中的资源
比如:
final ImmutableList.Builder此解决方案适用于我(DW 0.7.1):
我使用了一种更简单的方法来获取相同的数据。这里所有的资源都是泽西岛资源
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;
}
Map beansWithAnnotation=applicationContext.getBeansWithAnnotation(Path.class);
集合值=beansWithAnnotation.values();
用于(对象下一步:值){
ResourceUtil.getResourceUrls(下一步);
}
公共静态列表getResourceUrls(对象obj)
{
Resource=Resource.from(obj.getClass());
字符串uriPrefix=resource.getPath();
列表URL=新的ArrayList();
对于(资源res:Resource.getChildResources())
{
字符串uri=uriPrefix+res.getPath();
添加(uri);
}
返回URL;
}
此代码不包含任何扩展资源路径,也不包含类方法路径!
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;
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;
}