springboot生成接口文档

本文档将指导你如何在IntelliJ IDEA中创建一个Spring Initializr项目,并通过配置和实操步骤,利用Swagger来生成接口文档。首先,你需要在IDEA中选择新建Spring Initializr项目并配置JDK。接着,填写项目信息并确保URL正确。当项目下载完成后,添加pom.xml配置。在SwaggerConfig类中编写代码,并运行应用,通过http://localhost:8080/swagger-ui.html访问生成的接口文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步
1、打开idea
2、选择创建新的项目
在这里插入图片描述
3、选择创建Spring Lnitializer,然后点击next,当然你首先需要配置好jdk,jdk的安装和配置自行百度!
在这里插入图片描述
4、填写好信息,如果出现找不到url,请返回上一界面
在这里插入图片描述
4.1,找不到url,点击Cancel返回上一个界面,点击连接,就可以了,不可以你就选择Custom,找到官网的网址,复制那条链接粘贴到Custom下来

            5.这个页面提供你需要配置,自动生成,但我手动配置,你们可以根据增加的需要来配置

在这里插入图片描述
6.校验信息,没问题就下一步,就点生成
在这里插入图片描述
7.等待项目下载完成
二、添加pom.xml配置
在这里插入图片描述
三、配置和实操
1、创建包,类。名字随你们起
在这里插入图片描述
2、在config的SwaggerConfig添加代码,然后运行,打开
http://localhost:8080/swagger-ui.html就可以看到了

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket CreaeRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.test.testzhe.Controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                        .title("接口文档")
                        .description("这是一个接口文档的描述")
                        .version("2.0")
                        .contact(new Contact("嘻嘻","kkjj","123456789"))
                        .license("哈哈 ")
                        .licenseUrl("http://baidu.com")
                        .build();
    }

}
补充
在项目名称+Application 的类加上@EnableSwagger2这个注解
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210409142225773.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTI2NjIzNg==,size_16,color_FFFFFF,t_70)


附加问题集合地址
https://blog.youkuaiyun.com/weixin_45266236/article/details/102911457

### 如何在 Spring Boot 中生成 API 文档 #### 使用 Swagger 作为 API 文档工具 Swagger 是一种广泛使用的 API 文档生成功能强大的工具,在 Spring Boot 应用程序中可以方便地集成并使用。通过简单的配置和注解,Swagger 可以自动生成易于理解和导航的 RESTful Web Services 的交互式文档[^3]。 为了在 Spring Boot 项目中启用 Swagger 支持,首先需要向项目的 `pom.xml` 文件添加 Maven 依赖项: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 接着可以在应用的主要类上添加如下配置来启动 Swagger UI 和 Docket Bean: ```java import org.springframework.context.annotation.Bean; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .select() .apis(RequestHandlerSelectors.basePackage("com.example")) .paths(PathSelectors.any()) .build(); } } ``` 上述代码片段展示了如何创建一个名为 "api" 的 bean 来定义哪些包下的控制器应该被包含到生成的 API 文档里。 访问 http://localhost:8080/swagger-ui/index.html 即可查看由 Swagger 自动生成的 API 文档界面[^2]。 除了 Swagger 外,还有其他几种可用于生成 API 文档的选择,比如 Spring Rest Docs 或者 Enunciate 等等。每种工具有其特点,开发者可根据具体需求挑选最合适的方案[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值