springboot 集成swagger 步骤

Survive by day and develop by night.
talk for import biz , show your perfect code,full busy,skip hardness,make a better result,wait for change,challenge Survive.
happy for hardess to solve denpendies.

目录

概述

需求:

设计思路

实现思路分析

1.基础平台搭建

使用如下脚手架,创建基础环境,https://start.aliyun.com/bootstrap.html

2.步骤

要将Swagger集成到Spring Boot项目中,可以按照以下步骤进行操作:

  1. 添加Swagger依赖:在项目的Maven或Gradle配置文件中,添加Swagger依赖。
  • Maven:
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

或者

  <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
  • Gradle:
implementation 'io.springfox:springfox-boot-starter:3.0.0'
  1. 创建Swagger配置类:在项目的src/main/java目录下创建一个Swagger配置类,该类使用Swagger注解进行配置。
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.myproject.controllers"))
            .paths(PathSelectors.any())
            .build()
            .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("My Project API")
            .description("API documentation for My Project")
            .version("1.0.0")
            .build();
    }

}

2.9.2版本对应的是:

import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;



import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {
}
  1. 配置Swagger UI访问路径(可选):默认情况下,Swagger UI可以通过/swagger-ui/index.html路径访问,可以根据需要修改访问路径。
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    private ServletContext servletContext;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.myproject.controllers"))
            .paths(PathSelectors.any())
            .build()
            .apiInfo(apiInfo())
            .pathProvider(new RelativePathProvider(servletContext) {
                @Override
                public String getApplicationBasePath() {
                    return "/myproject"; // 修改访问路径为/myproject
                }
            });
    }

    // ...

}
  1. 运行项目并访问Swagger UI:启动Spring Boot项目后,可以通过访问Swagger UI的路径查看API文档。

默认访问路径是:http://localhost:8080/swagger-ui/index.html

在这里插入图片描述

访问url: http://localhost:9090/myapp/swagger-ui.html#/
在这里插入图片描述

以上就是将Swagger集成到Spring Boot项目的基本步骤。根据实际需要,还可以进行更多的配置,例如配置API文档的标题、描述、版本等信息。

参考资料和推荐阅读

参考资料
官方文档
开源社区
博客文章
书籍推荐

  1. https://blog.csdn.net/pleaseprintf/article/details/129945587

欢迎阅读,各位老铁,如果对你有帮助,点个赞加个关注呗!同时,期望各位大佬的批评指正~,如果有兴趣,可以加文末的交流群,大家一起进步哈

  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是Spring Boot集成Swagger的详细步骤与配置: 1. 在pom.xml文件中添加Swagger依赖 ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` 2. 创建Swagger配置类 创建一个SwaggerConfig类,并使用@EnableSwagger2注解开启Swagger功能。在Swagger配置类中,可以设置Swagger的一些基本信息,比如API文档的标题、描述、版本等。 ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build(); } } ``` 3. 配置Swagger UI 在application.properties文件中添加以下配置,以开启Swagger UI: ``` #Swagger UI springfox.documentation.swagger-ui.enabled=true springfox.documentation.swagger-ui.path=/swagger-ui.html ``` 4. 配置Swagger注解 在Controller层的方法上添加Swagger注解,以便生成API文档。常用的Swagger注解有: - @Api:用于修饰Controller类,表示这个类是Swagger资源; - @ApiOperation:用于修饰Controller类中的方法,表示一个HTTP请求的操作; - @ApiParam:用于修饰方法中的参数,表示对参数的描述; - @ApiImplicitParam:用于修饰方法中的参数,表示一个请求参数的配置信息; - @ApiModel:用于修饰响应类,表示一个返回响应的信息,比如响应的数据模型; - @ApiModelProperty:用于修饰响应类中的属性,表示对属性的描述。 例如: ``` @RestController @Api(value = "用户管理", tags = "用户管理API", description = "用户管理相关接口") public class UserController { @ApiOperation(value = "获取用户列表", notes = "获取所有用户信息") @GetMapping("/users") public List<User> getUserList() { // ... } @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息") @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long") @GetMapping("/users/{id}") public User getUser(@PathVariable Long id) { // ... } } ``` 5. 运行程序并访问Swagger UI 启动Spring Boot项目后,在浏览器中输入http://localhost:8080/swagger-ui.html,即可访问Swagger UI界面。在该界面中,可以查看API接口的详细信息、测试API接口等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值