自动将 Spring @RequestMapping 注释记录到一个位置? [英] Documenting Spring @RequestMapping annotations into one location automatically?

查看:30
本文介绍了自动将 Spring @RequestMapping 注释记录到一个位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javadoc 非常适合扫描所有源文件并创建 HTML 页面来查看它.我想知道是否有类似的工具可以遍历您的所有 Spring 控制器并收集所有已使用 @RequestMapping 注释的方法并生成一个列出它们的 HTML 页面.有点像开发人员的伪站点地图,以确保跨控制器的唯一性和标准化.

Javadoc is great for scanning all of source files and creating HTML pages to view it. I was wondering if there is a similar tool that would go through all of your Spring controllers and collect all of the methods that have been annotated with @RequestMapping and produce a single HTML page listing them. Sort of like a pseudo site map for developers to ensure uniqueness and standardization across controllers.

如果这个问题已经在其他地方被问过,我深表歉意.我无法想出一组合适的搜索词来提供有用的结果.

I apologize if this question has been asked elsewhere already. I could not come up with an appropriate set of search terms that would provide a useful result.

推荐答案

这是一个很好的问题,我经常错过(并实现)这样的功能.

This is a very good question, I often miss (and implement) functionality like this.

我要做的是运行 Maven(或 ant)并在编译后执行任务

What I'd do is run Maven (or ant) and execute a task after compilation that

  • 读取所有类(可能带有可配置的包列表)
  • 迭代这些类的所有方法
  • 读取注释
  • 并将输出写入 HTML

但我猜这是一个场景,其中注解处理 也可能是一种方法.通常,您必须使用一些内部 API 来完成 API 中的工作,但使用 Filer.createResource(...) 实际上应该可以开箱即用.

But I guess this is a scenario, where annotation processing might also be a way to do it. Usually, you have to use some internal APIs to get stuff done in API, but using Filer.createResource(...) it should actually possible to do it out of the box.

这是一个基本的实现:

public class RequestMappingProcessor extends AbstractProcessor{

    private final Map<String, String> map =
        new TreeMap<String, String>();

    private Filer filer;

    @Override
    public Set<String> getSupportedAnnotationTypes(){
        return Collections.singleton(RequestMapping.class.getName());
    }

    @Override
    public synchronized void init(
        final ProcessingEnvironment processingEnv){
        super.init(processingEnv);
        filer = processingEnv.getFiler();
    }

    @Override
    public boolean process(
        final Set<? extends TypeElement> annotations,
        final RoundEnvironment roundEnv){

        for(final TypeElement annotatedElement : annotations){
            final RequestMapping mapping =
                annotatedElement.getAnnotation(
                    RequestMapping.class
                );
            if(mapping != null){
                addMapping(mapping, annotatedElement, roundEnv);
            }
        }
        assembleSiteMap();
        return false;
    }

    private void assembleSiteMap(){
        Writer writer = null;
        boolean threw = false;
        try{
            final FileObject fileObject =
                filer.createResource(
                    StandardLocation.CLASS_OUTPUT,
                    "html", "siteMap.html"
                );
            writer = fileObject.openWriter();
            writer.append("<body>\n");
            for(final Entry<String, String> entry : map.entrySet()){
                writer
                    .append("<a href=\"")
                    .append(entry.getKey())
                    .append("\">")
                    .append("Path: ")
                    .append(entry.getKey())
                    .append(", method: ")
                    .append(entry.getValue())
                    .append("</a>\n");
            }
            writer.append("</body>\n");

        } catch(final IOException e){
            threw = true;
            throw new IllegalStateException(e);
        } finally{

            // with commons/io: IOUtils.closeQuietly(writer)
            // with Guava: Closeables.close(writer, rethrow)
            // with plain Java this monstrosity:
            try{
                if(writer != null){
                    writer.close();
                }
            } catch(final IOException e){
                if(!threw){
                    throw new IllegalStateException(e);
                }
            } finally{
            }
        }
    }

    private void addMapping(final RequestMapping mapping,
        final TypeElement annotatedElement,
        final RoundEnvironment roundEnv){
        final String[] values = mapping.value();
        for(final String value : values){
            map.put(
                value,
                annotatedElement.getQualifiedName().toString()
            );
        }
    }

}

这篇关于自动将 Spring @RequestMapping 注释记录到一个位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆