Skip to content

Instantly share code, notes, and snippets.

@tc-turner
Last active October 31, 2017 13:03
Show Gist options
  • Save tc-turner/1fbaec1663c30aec8951 to your computer and use it in GitHub Desktop.
Save tc-turner/1fbaec1663c30aec8951 to your computer and use it in GitHub Desktop.
List all available API endpoints in a JBoss/RestEasy application.

List all endpoints in JBoss/RestEasy Application

Add ApiResources.java to your project and make sure it is loaded at startup.

Navigate to /v2/listservices

This will result in output similar to this:

{
    "path": "/v2/posts",
    "methods": [
      "GET"
    ],
    "params": [
      {
        "name": "owner",
        "type": "String",
        "defaultval": ""
      },
      {
        "name": "tagLine",
        "type": "String",
        "defaultval": ""
      },
      {
        "name": "isDraft",
        "type": "Boolean",
        "defaultval": "false"
      }
    ]
  }
/**
* Created by mturner on 3/18/16.
*/
package com.cyber2.tc.api.v2.service;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.ObjectMapper;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.core.ResourceInvoker;
import org.jboss.resteasy.core.ResourceMethod;
import org.jboss.resteasy.core.ResourceMethodRegistry;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Stateless
@Path("/v2/listservices")
@Consumes(
{
"application/json", "application/xml"
})
@Produces(
{
"application/json", "application/xml"
})
public class ApiResources {
@Context
Dispatcher dispatcher;
@GET
@Path("/")
public Response getAllResources() {
List<EndPoint> resources = new ArrayList<>();
ResourceMethodRegistry registry = (ResourceMethodRegistry) dispatcher.getRegistry();
for (Map.Entry<String, List<ResourceInvoker>> entry : registry.getRoot().getBounded().entrySet()) {
for (ResourceInvoker invoker : entry.getValue()) {
ResourceMethod method = (ResourceMethod) invoker;
if (method.getMethod().getDeclaringClass() == getClass()) {
continue;
}
EndPoint endPoint = new EndPoint();
for (String verb : method.getHttpMethods()) {
endPoint.methods.add(verb);
endPoint.path = entry.getKey();
Method method1 = method.getMethod();
Parameter[] parameters = method1.getParameters();
for (final Parameter param : parameters) {
Param apiparam = new Param();
if (param.isAnnotationPresent(QueryParam.class)){
apiparam.type = param.getType().getSimpleName();
apiparam.name = param.getAnnotation(QueryParam.class).value();
if (param.isAnnotationPresent(DefaultValue.class)){
apiparam.defaultval = param.getAnnotation(DefaultValue.class).value();
}
endPoint.params.add(apiparam);
}
}
resources.add(endPoint);
}
}
}
ObjectMapper om = new ObjectMapper();
om.setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);
try {
return Response.ok(om.writeValueAsString(resources)).type(MediaType.APPLICATION_JSON_TYPE).build();
} catch (IOException e) {
return Response.ok(e.toString()).build();
}
}
}
class Param {
String name;
String type;
String defaultval;
}
class EndPoint {
String path;
List<String> methods;
List<Param> params;
public EndPoint(){
methods = new ArrayList<>();
params = new ArrayList<>();
}
}
@Scuilion
Copy link

In new versions of RESTEasy, ResourceMethod has become ResourceMethodInvoker.

@filosofisto
Copy link

Where is pom.xml?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment