在开发阶段显示可用的REST资源

时间:2014-07-20 16:23:39

标签: rest resteasy

我想知道可以将Java EE Web应用程序的可用REST路径(war deplopyment)输出为页面上的摘要。当然,出于安全原因,仅在开发模式下。有什么可用的吗?

由于

2 个答案:

答案 0 :(得分:1)

这是一个快速+脏的示例,它将返回扫描的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();
    }
}

答案 1 :(得分:0)

对于使用Eclipse的开发人员。只需使用打开Project Exlorer视图,查看JAX-RS Web服务下的可用资源列表。我很肯定其他IDE也有类似的东西。

enter image description here