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

sha*_*i15 5 java rest jax-rs jersey-1.0 jersey-2.0

我编写了自己的扫描程序来浏览我的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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*tha 7

Jersey 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();
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

这就是我能够提出的,与你所得到的相匹配.

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);        
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)