Display available REST resources in development stage

半腔热情 提交于 2019-12-24 07:35:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!