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

查看:611
本文介绍了记录春@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(或蚂蚁)和编译后执行一个任务

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


  • 读取所有类(也许包的配置清单)

  • 在这些类的所有方法迭代

  • 读取注释

  • 并写入输出HTML

不过,我想这是一个场景,其中注解处理也可能是一个办法做到这一点。通常情况下,你必须使用一些内部API得到的东西在API来完成,但使用<一个href=\"http://download.oracle.com/javase/6/docs/api/javax/annotation/processing/Filer.html#createResource%28javax.tools.JavaFileManager.Location%2C%20java.lang.CharSequence%2C%20java.lang.CharSequence%2C%20javax.lang.model.element.Element...%29\"相对=nofollow> 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.

下面是一个基本的实现:

Here's a rudimentary implementation:

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()
            );
        }
    }

}

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

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