我已经编写了自己的扫描器来浏览我的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)
  • AbstractResourceMethod == 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();
        }
    }
    

    您还可以使用枚举ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR来检查它是否等于ResourceMethod.getType()
    if (resourceMethod.getType()
            .equals(ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR) {
    
    }
    

  • 这就是我能想到的,以匹配您所得到的。
    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);
                }
            }
        }
    }
    

    09-10 06:32
    查看更多