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

相关推荐
有梦想的攻城狮3 天前
maven中的maven-resources-plugin插件详解
java·maven·插件·maven插件·maven-resources
胡斌附体6 天前
vue组件和插件的区别
vue.js·组件·插件·vue.component·vue.use
君莫愁。16 天前
【Unity】DOTween的常用函数解释
unity·c#·游戏引擎·插件·dotween
帆张芳显1 个月前
前端EXCEL插件,智表ZCELL产品V3.0 版本发布,底层采用canvas全部重构,功能大幅扩展,性能极致提升,满足千万级单元格加载
前端·重构·excel·jquery·插件·智表
机构师1 个月前
<uniapp><插件><UTS>在uniapp中,创建自己的插件并发布到uni插件市场
javascript·uni-app·vue·插件·hbuilderx·uni
亿坊电商1 个月前
开源CMS系统的SEO优化功能主要依赖哪些插件?
开源·cms·插件
啵啵学习1 个月前
浏览器插件,提示:此扩展程序未遵循 Chrome 扩展程序的最佳实践,因此已无法再使用
前端·chrome·浏览器·插件·破解
掘金詹姆斯1 个月前
在线接口调试工具-swagger
java·swagger
小巫程序Demo日记1 个月前
自问自答模式(Operation是什么)
swagger
装疯迷窍_A2 个月前
ARCGIS国土超级工具集1.5更新说明
arcgis·插件·变更调查·电子报盘·批量分割矢量