Introspecting Jersey resource model Jersey 2.x

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() + "\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. 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);
        }
    }
}