问题
I was wondering wheather it's possible to output the available REST paths of a Java EE web app (war deplopyment) as a summary on a page. Of course, for security reasons only in development mode. Is there something available for this?
Thanks
回答1:
Here is a quick + dirty example which will return all paths for the scanned ResourceClasses:
Path("/paths")
public class PathResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response paths(@Context HttpServletRequest request) {
StringBuilder out = new StringBuilder();
String applicationPath = "/"; // the path your Application is mapped to
@SuppressWarnings("unchecked")
Map<String, ResteasyDeployment> deployments = (Map<String, ResteasyDeployment>) request.getServletContext().getAttribute("resteasy.deployments");
ResteasyDeployment deployment = deployments.get(applicationPath);
List<String> scannedResourceClasses = deployment.getScannedResourceClasses();
try {
for (String className : scannedResourceClasses) {
Class<?> clazz = Class.forName(className);
String basePath = "";
if (clazz.isAnnotationPresent(Path.class)) {
basePath = clazz.getAnnotation(Path.class).value();
}
out.append(String.format("BasePath for Resource '%s': '%s'", className, basePath)).append('\n');
for (Method method : clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(Path.class)) {
String path = method.getAnnotation(Path.class).value();
out.append(String.format("Path for Method '%s': '%s'", method.getName(), basePath + path)).append('\n');
}
}
}
} catch(ClassNotFoundException ex) {
throw new IllegalArgumentException(ex);
}
return Response.ok(out).build();
}
}
回答2:
For developers who are working with Eclipse. Simply use open the Project Exlorer view and see the list of available resources under JAX-RS Web Services. I'm positive there is something similar for other IDEs.
来源:https://stackoverflow.com/questions/24852488/display-available-rest-resources-in-development-stage