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

时间:2015-04-15 15:47:55

标签: java rest jax-rs jersey-2.0 jersey-1.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);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:7)

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

我能想到的一些等价物:

  • AbstractResource == Resource

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

  • AbstracResourceMethod == ResourceMethod

  • resource.getSubResourceMethods() == getChildResources(),实际上只返回{​​{1}}

  • List<Resource> ==似乎不存在。我们只需检查上面的子资源,看它是否是一个定位器

    AbstractSubResourceLocator

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

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

答案 1 :(得分:3)

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