2015-04-15 290 views
3

我写了自己的扫描仪来浏览我的JAX-RS资源并使用jersey-server-1.18.1打印出方法名称和路径。问题是,当我将相同的代码迁移到2.16(将包名从com.sun.*更改为org.glassfish.*)时,它不起作用。内省泽西岛资源模型泽西岛2.x

深挖我发现那些所需的jersey-server类没有长期公开。任何人都知道原因?我该如何将我的代码从1.x迁移到2.x?这个迁移实际上没有文档。

所有帮助表示感谢!下面是一个1.x的

import com.wordnik.swagger.annotations.ApiOperation; 
import com.sun.jersey.api.model.AbstractResource; 
import com.sun.jersey.api.model.AbstractResourceMethod; 
import com.sun.jersey.api.model.AbstractSubResourceLocator; 
import com.sun.jersey.api.model.AbstractSubResourceMethod; 
import com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
/** 
* 
* @author shivang 
*/ 
public class Apiscanner { 
    public static void main(String[] args) { 
     Apiscanner runClass = new Apiscanner(); 
     runClass.xyz(); 
    } 

    public void xyz() { 
     AbstractResource resource = IntrospectionModeller.createResource(BaseResource.class); 
     String uriPrefix = resource.getPath().getValue(); 
     abc(uriPrefix, resource); 
    } 

    public void abc(String uriPrefix, AbstractResource resource) { 
     for (AbstractResourceMethod srm : resource.getResourceMethods()) { 
      String uri = uriPrefix; 
      System.out.println(srm.getHttpMethod() + "\t" + uri); 
     } 
     for (AbstractSubResourceMethod srm : resource.getSubResourceMethods()) { 
      String uri = uriPrefix + srm.getPath().getValue(); 
      ApiOperation op = srm.getAnnotation(ApiOperation.class); 
      System.out.println(srm.getHttpMethod() + "\t" + uri); 
     } 
     if (resource.getSubResourceLocators() != null && !resource.getSubResourceLocators().isEmpty()) { 
      for (AbstractSubResourceLocator subResourceLocator : resource.getSubResourceLocators()) { 
       ApiOperation op = subResourceLocator.getAnnotation(ApiOperation.class); 
       AbstractResource childResource = IntrospectionModeller.createResource(op.response()); 
       String path = subResourceLocator.getPath().getValue(); 
       String pathPrefix = uriPrefix + path; 
       abc(pathPrefix, childResource); 
      } 
     } 
    } 
} 

回答

4

泽西岛2.x的新的API代码,可以主要在org.glassfish.jersey.server.model包中找到。

我能想到的一些等价物:

  • AbstractResource == Resource

  • IntrospectionModeller.createResource ==我相信Resource.from(BaseResource.class)

  • AbstracResourceMethod == ResourceMethod

  • resource.getSubResourceMethods() == getChildResources(),其实只是返回一个List<Resource>

  • AbstractSubResourceLocator ==好像不存在。我们将简单地检查上述子资源,看它是否是一个定位

    for (Resource childResource: resource.getChildResources()) { 
        if (childResource.getResourceLocator() != null) { 
         ResourceMethod method = childResource.getResourceLocator(); 
         Class locatorType = method.getInvocable().getRawResponseType(); 
        } 
    } 
    

这里是我能拿出,以一种符合你得到了什么。

import com.wordnik.swagger.annotations.ApiOperation; 
import org.glassfish.jersey.server.model.Resource; 
import org.glassfish.jersey.server.model.ResourceMethod; 

public class ApiScanner { 
    public static void main(String[] args) { 
     ApiScanner scanner = new ApiScanner(); 
     scanner.xyz(); 
    } 

    public void xyz() { 
     Resource resource = Resource.from(BaseResource.class); 
     abc(resource.getPath(), resource); 
    } 

    public void abc(String uriPrefix, Resource resource) { 
     for (ResourceMethod resourceMethod: resource.getResourceMethods()) { 
      String uri = uriPrefix; 
      System.out.println("-- Resource Method --"); 
      System.out.println(resourceMethod.getHttpMethod() + "\t" + uri); 
      ApiOperation api = resourceMethod.getInvocable().getDefinitionMethod() 
               .getAnnotation(ApiOperation.class); 
     } 

     for (Resource childResource: resource.getChildResources()) { 
      System.out.println("-- Child Resource --"); 
      System.out.println(childResource.getPath() + "\t" + childResource.getName()); 

      if (childResource.getResourceLocator() != null) { 
       System.out.println("-- Sub-Resource Locator --"); 
       ResourceMethod method = childResource.getResourceLocator(); 
       Class locatorType = method.getInvocable().getRawResponseType(); 
       System.out.println(locatorType); 
       Resource subResource = Resource.from(locatorType); 
       abc(childResource.getPath(), subResource);   
      } 
     } 
    } 
} 
+0

感谢您的回复!我其实也只是把它工作。我也提供我的答案,以防人们想要重用它 – shahshi15

2

好的。所以我几乎可以在@peeskillet提供答案的同时使它工作。如果人们想重复使用代码,我会添加一个不同的答案风格:

import java.util.ArrayList; 
import java.util.List; 
import org.glassfish.jersey.server.model.Resource; 
import org.glassfish.jersey.server.model.ResourceMethod; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
/** 
* 
* @author shivang 
*/ 
public class JerseyResourceScanner { 
    public static void main(String[] args) { 
     JerseyResourceScanner runClass = new JerseyResourceScanner(); 
     runClass.scan(BaseResource.class); 
    } 

    public void scan(Class baseClass) { 
     Resource resource = Resource.builder(baseClass).build(); 
     String uriPrefix = ""; 
     process(uriPrefix, resource); 
    } 

    private void process(String uriPrefix, Resource resource) { 
     String pathPrefix = uriPrefix; 
     List<Resource> resources = new ArrayList<>(); 
     resources.addAll(resource.getChildResources()); 
     if (resource.getPath() != null) { 
      pathPrefix = pathPrefix + resource.getPath(); 
     } 
     for (ResourceMethod method : resource.getAllMethods()) { 
      if (method.getType().equals(ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR)) { 
       resources.add(
         Resource.from(resource.getResourceLocator() 
           .getInvocable().getDefinitionMethod().getReturnType())); 
      } 
      else { 
       System.out.println(method.getHttpMethod() + "\t" + pathPrefix); 
      } 
     } 
     for (Resource childResource : resources) { 
      process(pathPrefix, childResource); 
     } 
    } 
}