Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

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

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.

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() + "" + uri);
        }
        for (AbstractSubResourceMethod srm : resource.getSubResourceMethods()) {
            String uri = uriPrefix + srm.getPath().getValue();
            ApiOperation op = srm.getAnnotation(ApiOperation.class);
            System.out.println(srm.getHttpMethod() + "" + 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);
            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 == I believe Resource.from(BaseResource.class)

  • AbstractResourceMethod == ResourceMethod

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

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

    You can also use the enum ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR to check if it equals the 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() + "" + uri);
            ApiOperation api = resourceMethod.getInvocable().getDefinitionMethod()
                                                .getAnnotation(ApiOperation.class);
        }

        for (Resource childResource: resource.getChildResources()) {
            System.out.println("-- Child Resource --");
            System.out.println(childResource.getPath() + "" + 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);        
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...