当前位置: 首页 > news >正文

springboot生成前后端接口文档 - f

效果:
image
图片中是访问接口文档的路径
一、基本配置
1.pom.xml中导入依赖:(根据需要的版本导入)

<!-- swagger api文档 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></dependency><!--swagger ui--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></dependency>

2.在启动类添加注解@EnableSwagger2

package com.xq;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication
@EnableSwagger2//开启对所有控制器的扫描
public class GYMServiceWebApplication {public static void main(String[] args) {SpringApplication.run(GYMServiceWebApplication.class, args);}
}

3.编写配置信息(不重要)

package com.xq.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig{/*** 创建Docket类型的对象,并使用Spring容器管理* Docket是Swagger中的全局配置对象* @return*/@Beanpublic Docket getDocket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(swaggerDemoApiInfo()).select().build();}private ApiInfo swaggerDemoApiInfo(){return new ApiInfoBuilder().contact(new Contact("JAVA学习社区","https://www.qq.com","eric@qq.com")) //配置文档主体内容
//文档标题.title("这里是Swagger的标题")
//文档描述.description("这里是Swagger的描述")
//文档版本.version("1.0.0").build();}
}

二、自定义注解和元注解

package com.xq.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 编写自定义注解,一定要加上元注解信息,所谓的元注解就是我们再定义注解的时候必须携带的注解信息,参考springmvc的注解。* 常见的元注解:* @Target:描述的是当前注解需要修饰哪个目标资源.*      @Target(ElementType.TYPE) 描述当前注解可以修饰类或接口*      @Target(ElementType.FIELD) 当前注解可以修饰成员变量*      @Target(ElementType.METHOD)  当前注解可以修饰方法*      @Target(ElementType.PARAMETER) 当前注解可以修饰方法参数*  ......* @Inherited  被修饰的注解具有继承性* @Documented  指的是被修饰的注解可以被javadoc工具提取成文档* @Retention  定义注解的生命周期(保留策略)*      @Retention(RetentionPolicy.SOURCE) 当前注解只会存在源码中,不会被编译,更不会被运行*      @Retention(RetentionPplicy.CLASS)  当前注解会参与编译,但是不会参与运行*      @Retention(RetentionPolicy.RUNTIME)  当前注解既参与编译,有参与运行,在运行时我们会基于反射的机制获取到该注解信息*/@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface NoIncludeSwagger {
}

自定义注解如何起作用:(该注解的作用:不需要生成对应控制器方法的接口文档)
image

http://www.rkmt.cn/news/51484.html

相关文章:

  • AI元人文:价值权衡的双模引擎与五维元问——构建人机共生的存在语法
  • Spring Cloud - Spring Cloud 注册中心与服务提供者(Spring Cloud Eureka 概述、微服务高效入门、微服务应用实例)
  • DateUtil
  • (链表)判断是否回文
  • (链表)判断两个单链表是否存在交点
  • (链表)任意删除一个结点
  • 在抖音直播推广开源作品的可行性?
  • DLSS Swapper商业模式:开源软件商业化探索 - 指南
  • irm steam.work|iex 风险分析
  • Pandas --DataFrame基本操作
  • 2025年11月全国旗杆厂家综合实力排行榜TOP5权威发布
  • 入侵防护技术深度解析:最新漏洞与威胁态势
  • 20232427 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • 解决Elctron打包成功,IPC无法注册问题。
  • 在Windows系统置顶窗口不被Win+D快捷键影响
  • 点分树
  • HTTP请求走私漏洞介绍 - 实践
  • 深入解析:Spring MVC 拦截器interceptor
  • 《重生之我成为世界顶级黑客》第八章:未来野望
  • 打开工作空间时,但未在 DTD/架构中声明
  • 从 LLM 到 Agentic AI:构建下一代智能平台的全栈路径
  • 20232418 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • Claude Code教程:从零构建AutoPost GPT自动内容生成系统
  • python多进程 —— multiprocessing.Manager —— 跨主机共享内存的读写
  • 详细介绍:UVa 11129 An Antiarithmetic Permutation
  • 3天掌握OpenHarmony+Python开发:高效适配教程与真实项目案例精讲 - 教程
  • 25.11.15随笔联考总结(补)
  • 《重生之我成为世界顶级黑客》第六章:一线生机
  • 遥感建筑物变化检测内容集
  • 【UE源码向】GameplayTag增加ToolTip