swagger3快速使用

目录

🍿1.导入依赖

🌭2.添加配置文件

🧂3.添加注解

🥯4.访问客户端


1.导入依赖

引入swagger3的依赖包

XML 复制代码
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>3.0.0</version>
            </dependency>

2.添加配置文件

  • 创建swagger的配置,创建Docket对象,并构建相应的信息
  • 注意:一定使用@EnableOpenApi开启swagger文档
java 复制代码
@Component
@Data
@EnableOpenApi//开启swagger文档
public class SwaggerConfiguration {

    @Bean
    public Docket webApiDoc(){
        return new Docket(DocumentationType.OAS_30)
                .groupName("用户端接口文档")
                .pathMapping("/")
                //是否开启swagger
                .enable(true)
                //配置文档元信息
                .apiInfo(apiInfo())
                .select()
                //扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.xz"))
                //正则匹配请求路径
                .paths(PathSelectors.ant("/api/**"))
                .build();
    }

    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("小张电商平台")
                .description("微服务接口文档")
                .contact(new Contact("会敲代码的小张","https://hqdmdxz","微信:886"))
                .version("v1.0")
                .build();
    }
}

3.添加注解

  • 在controller层添加相应的注解
  • 使用@api:表示模块信息
  • 使用@ApiOperation:表示接口的具体信息说明
  • 使用@ApiParam:表示参数的说明
java 复制代码
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("根据id查询用户信息")
    @GetMapping("/getUserById/{id}")
    public UserDO getUserById(@ApiParam(value = "用户id", required = true)
                              @PathVariable("id") Long id) {
        UserDO user = userService.getById(id);
        return user;
    }
}

4.访问客户端

在浏览器访问Swagger3的UI页面:自己的项目地址/swagger-ui/index.html

相关推荐
翻滚吧键盘18 天前
zsh安装插件
zsh·插件
梦想画家21 天前
Golang Gin系列-9:Gin 集成Swagger生成文档
golang·gin·swagger
装疯迷窍_A1 个月前
ARCGIS国土超级工具集1.3更新说明
arcgis·c#·插件·变更调查·尖锐角·狭长
zfj3211 个月前
学英语学技术: jmeter插件管理器
jmeter·插件·压测工具
xiangxiongfly9151 个月前
Vue3 自定义插件(plugin)
vue3·插件·plugin
drebander2 个月前
在 IntelliJ IDEA 中开发 GPT 自动补全插件
intellij-idea·插件
灰色孤星A2 个月前
瑞吉外卖项目学习笔记(二)Swagger、logback、表单校验和参数打印功能的实现
springboot·logback·swagger·瑞吉外卖·切面编程·表单校验·黑马程序员
Alan_Wdd2 个月前
解决谷歌人机验证 (reCAPTCHA) 无法加载问题
前端·chrome·浏览器·插件·人机验证·recaptcha
亦世凡华、2 个月前
从零开始:如何在.NET Core Web API中完美配置Swagger文档
开发语言·c#·swagger·.net core·web api
哆啦 AI 梦2 个月前
【Maven】生命周期和插件详解
maven·生命周期·插件·插件目标