内省泽西资源模型泽西2.x.

我编写了自己的扫描程序来浏览我的JAX-RS资源,并使用jersey-server-1.18.1打印出方法名称和路径。 问题是当我将相同的代码迁移到2.16(将包名称从com.sun.*更改为org.glassfish.* )时,它将无法正常工作。

深入挖掘我发现那些必需的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); } } } } 

Jersey 2.x的新API主要可以在org.glassfish.jersey.server.model包中找到。

我能想到的一些等价物:

  • AbstractResource == Resource

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

  • AbstracResourceMethod == ResourceMethod

  • resource.getSubResourceMethods() == getChildResources() ,它实际上只返回一个List

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

好。 所以我几乎可以在@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 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); } } }