内省 Jersey 资源模型 Jersey 2.x

Introspecting Jersey resource model Jersey 2.x

我已经编写了自己的扫描器来检查我的 JAX-RS 资源并使用 jersey-server-1.18.1 打印出方法名称和路径。问题是当我将我的相同代码迁移到 2.16 时(将包名称从 com.sun.* 更改为 org.glassfish.*),它就是行不通。

深入挖掘发现那些需要的 jersey-server 类 并不长 public。任何人都知道为什么?我如何将下面的代码从 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(),其实就是returns一个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);        
            }
        }
    }
}

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