Java 使用Jersey枚举资源
我使用Jersey 1.17提供REST web服务 我知道Jersey“知道”所有的资源是什么,那么有没有办法让Jersey给我一份它发现的资源的完整列表 背景:我想让用户能够在资源级别配置用户/权限。为此,我想列举Jersey发现的所有资源,以填充用户界面,为用户设置安全性。然后,我将在数据库中保留用户/URL权限 解决方案Java 使用Jersey枚举资源,java,security,rest,jersey,Java,Security,Rest,Jersey,我使用Jersey 1.17提供REST web服务 我知道Jersey“知道”所有的资源是什么,那么有没有办法让Jersey给我一份它发现的资源的完整列表 背景:我想让用户能够在资源级别配置用户/权限。为此,我想列举Jersey发现的所有资源,以填充用户界面,为用户设置安全性。然后,我将在数据库中保留用户/URL权限 解决方案 static class ResourceListBuilder { private List<ResourceInfo> resourceInfo
static class ResourceListBuilder
{
private List<ResourceInfo> resourceInfos = new ArrayList<ResourceInfo>();
public List<ResourceInfo> getResourceInfos()
{
return resourceInfos;
}
private void build( Application application )
{
for ( Class<?> aClass : application.getClasses() )
{
if ( isAnnotatedResourceClass( aClass ) )
{
AbstractResource resource = IntrospectionModeller.createResource( aClass );
buildResource( resource, resource.getPath().getValue() );
}
}
}
private void buildResource( AbstractResource resource, String uriPrefix )
{
for ( AbstractSubResourceMethod srm : resource.getSubResourceMethods() )
{
String uri = uriPrefix + srm.getPath().getValue();
resourceInfos.add( new ResourceInfo( uri, srm.getHttpMethod(), srm.getMethod().getName() ) );
}
for ( AbstractResourceMethod srm : resource.getResourceMethods() )
{
resourceInfos.add( new ResourceInfo( uriPrefix, srm.getHttpMethod(), srm.getMethod().getName() ) );
}
for ( AbstractSubResourceLocator locator : resource.getSubResourceLocators() )
{
AbstractResource locatorResource = IntrospectionModeller.createResource( locator.getMethod().getReturnType() );
buildResource( locatorResource, uriPrefix + locator.getPath().getValue() );
}
}
private boolean isAnnotatedResourceClass( Class rc )
{
if ( rc.isAnnotationPresent( Path.class ) )
{
return true;
}
for ( Class i : rc.getInterfaces() )
{
if ( i.isAnnotationPresent( Path.class ) )
{
return true;
}
}
return false;
}
}
static class ResourceInfo
{
private String url;
private String method;
private String description;
ResourceInfo( String url, String method, String description )
{
this.url = url;
this.method = method;
this.description = description;
}
public String getUrl()
{
return url;
}
public String getMethod()
{
return method;
}
public String getDescription()
{
return description;
}
}
静态类ResourceListBuilder
{
private List resourceInfos=new ArrayList();
公共列表getResourceInfos()
{
返回资源信息;
}
专用void构建(应用程序)
{
对于(类aClass:application.getClasses())
{
if(isAnnotatedResourceClass(aClass))
{
AbstractResource=introspectionModeler.createResource(aClass);
buildResource(resource,resource.getPath().getValue());
}
}
}
私有void buildResource(AbstractResource资源,字符串前缀)
{
for(AbstractSubResourceMethod srm:resource.getSubResourceMethods())
{
字符串uri=uriPrefix+srm.getPath().getValue();
添加(新的ResourceInfo(uri,srm.getHttpMethod(),srm.getMethod().getName());
}
for(AbstractResourceMethod srm:resource.getResourceMethods())
{
添加(新的ResourceInfo(uriPrefix,srm.getHttpMethod(),srm.getMethod().getName());
}
for(AbstractSubResourceLocator定位器:resource.getSubResourceLocators())
{
AbstractResourceLocatorResource=introspectionModeler.createResource(locator.getMethod().getReturnType());
buildResource(locatorResource,uriPrefix+locator.getPath().getValue());
}
}
私有布尔值isAnnotatedResourceClass(类rc)
{
if(rc.isAnnotationPresent(Path.class))
{
返回true;
}
对于(类i:rc.getInterfaces())
{
if(i.isAnnotationPresent(Path.class))
{
返回true;
}
}
返回false;
}
}
静态类ResourceInfo
{
私有字符串url;
私有字符串方法;
私有字符串描述;
ResourceInfo(字符串url、字符串方法、字符串描述)
{
this.url=url;
这个方法=方法;
this.description=描述;
}
公共字符串getUrl()
{
返回url;
}
公共字符串getMethod()
{
返回法;
}
公共字符串getDescription()
{
返回说明;
}
}
使用Jersy提出了一个很好的解决方案
该解决方案展示了如何开发服务,列出可以使用cURL命令调用的所有已部署资源。谢谢您的帮助。但是,发布的链接解决方案缺少处理子资源定位器。我已经发布了我的完整解决方案