springBoot整合swagger-bootstrap-ui实现接口文档的生成查看功能

mac2025-02-16  9

前言

在开发完rest资源项目后,我们一般要提供接口文档作为接口调用的说名,每次手动维护接口文档的工作会和繁琐,所以使用swagger-bootstrap-ui来实现项目接口的为维护

Swagger 常用注解使用详解

加入maven的依赖

在pom.xml文件中添加

<!-- swagger2 和 swagger2 依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency>

Swagger的配置

package com.kcsm.product.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * 描述:Swagger的配置 * * @Auther: lzx * @Date: 2019/9/23 09:11 */ @Configuration @EnableSwagger2 /* 接口文档的作用范围 -- 只在开发环境使用 等,下面的值何配置中定义的spring.profiles.active有/*关 */ @Profile({"local","dev"}) public class SwaggerConfiguration { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //扫描的包 .apis(RequestHandlerSelectors.basePackage("com.xxx.xxx")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("商品服务接口") .description("商品服务接口") .termsOfServiceUrl("http://www.xxx.cn/xxx/") .contact(new Contact("lzx","http://www.xxx.cn","lzx@qq.com")) //版本 .version("1.0") .build(); } }

配置文件

在application.properties或application.yml配置中加入环境标签声明

# 读取了配置 spring.profiles.active=${SPRING_PROFILE}

启动项目

启动项目,访问 :项目地址/doc.html 可以看到如下界面

最新回复(0)