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

相关推荐
sean9081 天前
Firefox 安装非市场的插件
firefox·插件·市场
召田最帅boy2 天前
Swagger使用教程
swagger
闲人编程6 天前
OpenAPI/Swagger规范与API文档自动化
运维·自动化·json·swagger·schema·openapi·codecapsule
wtsolutions7 天前
WPS另存为JSON,WPS导出JSON, WPS表格转换成JSON : Excel to JSON WPS插件使用指南
json·excel·wps·插件·加载项·wtsolutions
一念一花一世界7 天前
接口管理工具选型:Swagger与PostIn全面对比指南
测试工具·postman·swagger·postin·接口管理工具
一念一花一世界8 天前
接口管理工具选型:Postman、Swagger与PostIn的全面对比指南
测试工具·postman·swagger·接口管理工具
wtsolutions8 天前
JSON转Excel工具新增WPS插件功能,将JSON转换成WPS表格工作表数据
json·excel·wps·插件·转换·加载项·wtsolutions
wtsolutions9 天前
Excel to JSON by WTSolutions 4.0.0 版本更新公告
json·excel·wps·插件·转换·加载项·wtsolutions
avi911113 天前
发现一个宝藏Unity开源AVG框架,视觉小说的脚手架
unity·开源·框架·插件·tolua·avg
love530love14 天前
【笔记】ComfUI RIFEInterpolation 节点缺失问题(cupy CUDA 安装)解决方案
人工智能·windows·笔记·python·插件·comfyui