内省泽西资源模型泽西2.x. [英] Introspecting Jersey resource model Jersey 2.x

查看:69
本文介绍了内省泽西资源模型泽西2.x.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了自己的扫描程序来浏览我的JAX-RS资源,并使用 jersey-server-1.18.1 打印出方法名称和路径。问题是我将相同的代码迁移到2.16(将包名称从 com.sun。* 更改为 org.glassfish。* ),它不会起作用。

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. 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.

深入挖掘我发现那些必需 jersey-server 的课程并不公开。谁知道原因?如何将我的代码从1.x迁移到2.x?实际上没有关于此迁移的文档。

Digging deep I found that those required jersey-server classes are no long public. Anyone knows the reason why? And how can I migrate my code below from 1.x to 2.x ? There is literally no documentation on this migration.

所有帮助表示赞赏!下面的代码是1.x

All help appreciated! Below is the code with 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 package。

The new APIs for Jersey 2.x, can mainly be found in the org.glassfish.jersey.server.model package.

我能想到的一些等价物:

Some equivalents I can think of:

  • AbstractResource == Resource

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

AbstracResourceMethod == ResourceMethod

AbstracResourceMethod == ResourceMethod

res ource.getSubResourceMethods() == getChildResources(),实际上只返回列表<资源>

resource.getSubResourceMethods() == getChildResources(), which actually just returns a List<Resource>

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

AbstractSubResourceLocator == Doesn't seem to exist. 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();
    }
}


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

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

这篇关于内省泽西资源模型泽西2.x.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆