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

相关推荐
死磕音视频6 天前
ChatGPT 应用与插件入门:连接 Google Drive 并在对话中调用
chatgpt·插件·办公效率·应用连接
小锋学长生活大爆炸7 天前
【插件】像素风桌宠与Claude Code状态监控的完美结合 | Claude-Desktop-Pet
claude·插件·桌宠
ProcessOn官方账号8 天前
ProcessOn Office 插件上线:在 Word / Excel / PPT 里直接画流程图
word·excel·插件·office·processon
csdn_aspnet10 天前
C# .NetCore Swagger ISchemaFilter 处理枚举、特殊类型
c#·.netcore·swagger
csdn_aspnet10 天前
C# .NetCore Swagger ISchemaFilter Long 转 String 防精度丢失
c#·.netcore·swagger
csdn_aspnet11 天前
C# .NetCore Swagger ISchemaFilter 注入示例值、描述
c#·.netcore·swagger
信也科技布道师12 天前
把 Swagger 变成联调工作台:Swagger Pro 工程实践
swagger
青柠之夏cc15 天前
YCM插件与Vim9.1.0496+兼容性问题解析
插件·版本·兼容
我才是银古16 天前
DeepSeek Harness 插件生态解析
插件·deepseek·dsh