简体   繁体   English

内省 Jersey 资源模型 Jersey 2.x

[英]Introspecting Jersey resource model Jersey 2.x

I have written my own scanner to go through my JAX-RS resources and print out the method names and paths using jersey-server-1.18.1 .我已经编写了自己的扫描程序来浏览我的 JAX-RS 资源并使用jersey-server-1.18.1打印出方法名称和路径。 The problem is when I migrate my same code to 2.16 (changing the package names from com.sun.* to org.glassfish.* ), It just won't work.问题是当我将相同的代码迁移到 2.16(将包名从com.sun.*更改为org.glassfish.* )时,它就行不通了。

Digging deep I found that those required jersey-server classes are no long public.深入挖掘我发现那些必需的jersey-server类不再公开。 Anyone knows the reason why?有谁知道原因? And how can I migrate my code below from 1.x to 2.x ?以及如何将下面的代码从 1.x 迁移到 2.x ? There is literally no documentation on this migration.实际上没有关于此迁移的文档。

All help appreciated!所有帮助表示赞赏! Below is the code with 1.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);
            }
        }
    }
}

The new APIs for Jersey 2.x, can mainly be found in the org.glassfish.jersey.server.model package. Jersey 2.x 的新 API 主要可以在org.glassfish.jersey.server.model包中找到。

Some equivalents I can think of:我能想到的一些等价物:

  • AbstractResource == Resource AbstractResource Resource == Resource

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

  • AbstractResourceMethod == ResourceMethod AbstractResourceMethod ResourceMethod == ResourceMethod

  • resource.getSubResourceMethods() == getChildResources() , which actually just returns a List<Resource> resource.getSubResourceMethods() == getChildResources() ,它实际上只是返回一个List<Resource>

  • AbstractSubResourceLocator == Doesn't seem to exist. AbstractSubResourceLocator == 似乎不存在。 We would simply check the above child resource to see if it is a locator我们只需检查上面的子资源,看看它是否是一个定位器

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

    You can also use the enum ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR to check if it equals the ResourceMethod.getType()您还可以使用枚举ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR来检查它是否等于ResourceMethod.getType()

     if (resourceMethod.getType() .equals(ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR) { }

Here's what I was able to come up with, to kind of match what you got.这是我能够想出的,与你得到的相匹配。

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

OK.好的。 So I was able to get it to work almost at the same time as @peeskillet provided the answer.所以我几乎在@peeskillet 提供答案的同时让它开始工作。 I will add just a different flavor of the answer in case people want to reuse the code:如果人们想要重用代码,我将添加不同风格的答案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM