利用javadoc注释自动生成Swagger注解

 更新时间:2023年08月13日 09:20:50   作者:三火哥  
由于现在controller方法上面没有swagger注解,只能拿到接口url地址,无法获得接口功能描述,所以本文为大家介绍一下如何利用javadoc注释自动生成Swagger注解,感兴趣的可以了解下

目的

可视化接口管理平台,通过接口管理平台一目了然知道接口的作用,接口上面扩展了哪些功能,从而做到对应用程序代码零侵入

现状

由于现在controller方法上面没有swagger注解,只能拿到接口url地址,无法获得接口功能描述。

值得庆幸的是我们的代码非常规范每个方法都有标准的javadoc注释,于是就想到了获取javadoc注释的描述自动生成swagger注解

思路

1,AOP切面,你会发现根本无法获取javadoc,这个方案就直接pass掉了

2,读文件流获取文件内容解析出想要的数据,虽然可以但是费时费力,不是最优的方案

3,使用开源的javaparser难道不香吗,非常香就它了

javaparser

<dependency>
	<groupId>com.github.javaparser</groupId>
	<artifactId>javaparser-core</artifactId>
	<version>3.25.4</version>
</dependency>
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import com.fuchuang.scm.common.util.ScmAssert;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 通过javadoc注释自动生成Swagger注解
 *
 * @author xiongyan
 * @date 2023/7/7
 */
public class Sw {
    public static void main(String[] args) throws Exception {
        String clientPath = "/Users/xiongyan/Documents/work/fc-cdc/fc-cdc-api/src/main/java/com/fc/cdc/controller";
        String controllerPath = "/Users/xiongyan/Documents/work/fc-cdc/fc-cdc-api/src/main/java/com/fc/cdc/controller";
        Map<String, ClassOrInterfaceDeclaration> clientMap = Sw.clientMap(clientPath);
        File project = new File(controllerPath);
        ScmAssert.isTrue(project.exists(), "项目路径不存在");
        for (File file : project.listFiles()) {
            CompilationUnit cu = StaticJavaParser.parse(file);
            ClassOrInterfaceDeclaration classDeclaration = cu.findFirst(ClassOrInterfaceDeclaration.class).orElse(null);
            if (null == classDeclaration) {
                continue;
            }
            String classComment = null;
            Map<String, String> methodCommentMap = new HashMap<>();
            if (classDeclaration.getImplementedTypes().size() > 0) {
                String interfaceName = classDeclaration.getImplementedTypes(0).getNameAsString();
                ClassOrInterfaceDeclaration interfaceDeclaration = clientMap.get(interfaceName);
                if (null == interfaceDeclaration) {
                    continue;
                }
                if (interfaceDeclaration.getJavadoc().isPresent()) {
                    classComment = interfaceDeclaration.getJavadoc().get().getDescription().toText();
                }
                for (MethodDeclaration method : interfaceDeclaration.getMethods()) {
                    if (!method.getJavadoc().isPresent()) {
                        continue;
                    }
                    methodCommentMap.put(method.getNameAsString(), method.getJavadoc().get().getDescription().toText());
                }
            }
            if (StrUtil.isEmpty(classComment)) {
                if (classDeclaration.getJavadoc().isPresent()) {
                    classComment = classDeclaration.getJavadoc().get().getDescription().toText();
                } else {
                    classComment = classDeclaration.getNameAsString();
                }
            }
            if (!classDeclaration.getAnnotationByClass(Api.class).isPresent()) {
                classDeclaration.addAndGetAnnotation(Api.class).addPair("value", "\"" + classComment + "\"");
            }
            for (MethodDeclaration method : classDeclaration.getMethods()) {
                if (method.getAnnotationByClass(ApiOperation.class).isPresent()) {
                    continue;
                }
                String methodComment = methodCommentMap.get(method.getNameAsString());
                if (StrUtil.isEmpty(methodComment)) {
                    if (method.getJavadoc().isPresent()) {
                        methodComment = method.getJavadoc().get().getDescription().toText();
                    } else {
                        methodComment = method.getNameAsString();
                    }
                }
                method.addAndGetAnnotation(ApiOperation.class).addPair("value", "\"" + classComment + "-" + methodComment + "\"");
            }
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(cu.toString().getBytes());
        }
    }
    private static Map<String, ClassOrInterfaceDeclaration> clientMap(String path) throws FileNotFoundException {
        File project = new File(path);
        ScmAssert.isTrue(project.exists(), "项目路径不存在");
        Map<String, ClassOrInterfaceDeclaration> clientMap = new HashMap<>();
        for (File file : project.listFiles()) {
            CompilationUnit cu = StaticJavaParser.parse(file);
            if (null == cu) {
                continue;
            }
            ClassOrInterfaceDeclaration classDeclaration = cu.findFirst(ClassOrInterfaceDeclaration.class).orElse(null);
            if (null != classDeclaration && classDeclaration.isInterface()) {
                clientMap.put(classDeclaration.getNameAsString(), classDeclaration);
            }
        }
        return clientMap;
    }
}

效果

/**
 * 数据库管理
 *
 * @author xiongyan
 * @date 2023/03/13
 */
@RestController
@RequestMapping("/web/database")
public class WebDatabaseController {
    /**
     * 分页列表
     *
     * @param request
     * @return
     */
    @PostMapping("/page")
    public ScmResponse<ScmPage<DatabaseDto>> page(@RequestBody DatabaseRequest request) {
        return ScmResponseUtil.success();
    }
}
/**
 * 数据库管理
 *
 * @author xiongyan
 * @date 2023/03/13
 */
@RestController
@RequestMapping("/web/database")
@Api(value = "数据库管理")
public class WebDatabaseController {
    /**
     * 分页列表
     *
     * @param request
     * @return
     */
    @PostMapping("/page")
    @ApiOperation(value = "数据库管理-分页列表")
    public ScmResponse<ScmPage<DatabaseDto>> page(@RequestBody DatabaseRequest request) {
        return ScmResponseUtil.success();
    }
}

可以看到通过javadoc自动生成了swagger注解,尤其是对于我们这样微服务拆分很细的,应用非常多,接口更是多的,通过这种方式效率最高的

接口文档

服务器启动就可以把 接口应用名称,接口URL地址,接口请求方式,接口描述,接口请求结构,接口响应结构 数据收集进行注册自动生成接口文档

接口扩展

1,数据脱敏

2,操作日志

3,防重复提交

4,接口幂等

5,接口限流

6,黑白名单

7,权限控制

8,。。。。。

到此这篇关于利用javadoc注释自动生成Swagger注解的文章就介绍到这了,更多相关javadoc生成Swagger注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java父线程(或是主线程)等待所有子线程退出的实例

    Java父线程(或是主线程)等待所有子线程退出的实例

    下面小编就为大家分享一篇Java父线程(或是主线程)等待所有子线程退出的实例,具有很好的参考价值,希望对大家有所帮助
    2017-11-11
  • 使用通过ARP类似P2P终结者实现数据封包

    使用通过ARP类似P2P终结者实现数据封包

    目前网络上类似P2P终结者这类软件,主要都是基于ARP欺骗实现的,网络上到处都有关于ARP的介绍,不过为了本文读者不需要再去查找,我就在这里大概讲解一下
    2012-12-12
  • java异常和错误类总结(必看篇)

    java异常和错误类总结(必看篇)

    下面小编就为大家带来一篇java异常和错误类总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • Java Stream 流的使用过程解析

    Java Stream 流的使用过程解析

    这篇文章主要介绍了Java Stream 流的使用过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Java字符串格式化,{}占位符根据名字替换实例

    Java字符串格式化,{}占位符根据名字替换实例

    这篇文章主要介绍了Java字符串格式化,{}占位符根据名字替换实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • 确保SpringBoot定时任务只执行一次的常见方法小结

    确保SpringBoot定时任务只执行一次的常见方法小结

    在Spring Boot项目中,确保定时任务只执行一次是一个常见的需求,这种需求可以通过多种方式来实现,以下是一些常见的方法,它们各具特点,可以根据项目的实际需求来选择最合适的方法,需要的朋友可以参考下
    2024-10-10
  • 详解Java设计模式之职责链模式

    详解Java设计模式之职责链模式

    责任链模式是一种行为设计模式,使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,文中通过代码示例给大家介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • java返回的List进行add操作报错

    java返回的List进行add操作报错

    本文主要介绍了java返回的List进行add操作报错,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Hibernate核心思想与接口简介

    Hibernate核心思想与接口简介

    这篇文章主要介绍了Hibernate核心思想与接口的相关内容,需要的朋友可以参考下。
    2017-09-09
  • SpringBoot和MyBatis环境下实现动态数据源切换过程

    SpringBoot和MyBatis环境下实现动态数据源切换过程

    dynamic-datasource-spring-boot-starter 是一个用于在SpringBoot和MyBatis环境下实现动态数据源切换的工具,它简化了配置和切换逻辑,通过引入依赖,配置数据源和使用@DS注解,可以轻松实现数据源的动态切换
    2025-10-10

最新评论