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

相关推荐
七夜zippoe1 天前
DolphinDB开发环境:GUI与API工具
api·gui·插件·开发环境·dolphindb
曲幽6 天前
FastAPI自动生成的API文档太丑?我花了一晚上把它改成了客户愿意付费的样子
python·fastapi·web·swagger·openapi·scalar·docs
曲幽7 天前
告别手写 API 胶水代码:FastAPI 与 Vue 的“契约自动机” OpenAPI 实战
python·typescript·vue·fastapi·web·swagger·openapi·codegen
xiaobobo333012 天前
vscode的if结尾提示插件“If End Marker”实现了if结尾提示功能
vscode·插件·if结尾提示·if end marker
sleeper0115 天前
War3 Replay Overlay 技术实现原理
插件·war3·魔兽争霸3·replay
Zzxy16 天前
Spring Boot集成Swagger/Knife4j
spring boot·swagger
sam.li19 天前
GhidraMCP 原理与使用部署
ai·逆向·插件·mcp·ghidra
sqmw21 天前
MFCMouseEffect:把桌面输入反馈这件事,做成一个真正可扩展的引擎
c++·插件·引擎·鼠标特效·键鼠指示·鼠标伴宠
I'm Jie24 天前
Swagger UI 本地化部署,解决 FastAPI Swagger UI 依赖外部 CDN 加载失败问题
python·ui·fastapi·swagger·swagger ui
l1t25 天前
DeepSeek总结的用 C# 构建 DuckDB 插件说明
前端·数据库·c#·插件·duckdb