|
|
|
@ -1,6 +1,12 @@ |
|
|
|
|
package cc.bnblogs.swagger_study.config; |
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
import org.springframework.core.env.Environment; |
|
|
|
|
import org.springframework.core.env.Profiles; |
|
|
|
|
import springfox.documentation.builders.ParameterBuilder; |
|
|
|
|
import springfox.documentation.schema.ModelRef; |
|
|
|
|
import springfox.documentation.service.Parameter; |
|
|
|
|
import springfox.documentation.swagger2.annotations.EnableSwagger2; |
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
|
import springfox.documentation.builders.ApiInfoBuilder; |
|
|
|
@ -11,9 +17,12 @@ import springfox.documentation.service.Contact; |
|
|
|
|
import springfox.documentation.spi.DocumentationType; |
|
|
|
|
import springfox.documentation.spring.web.plugins.Docket; |
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @description: |
|
|
|
|
* @description: 配置Swagger |
|
|
|
|
* @author: zfp@bnblogs.cc |
|
|
|
|
* @date: 2023/3/5 0:00 |
|
|
|
|
*/ |
|
|
|
@ -21,17 +30,36 @@ import springfox.documentation.spring.web.plugins.Docket; |
|
|
|
|
@EnableSwagger2 |
|
|
|
|
@Configuration |
|
|
|
|
public class SwaggerConfig { |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
public Docket createRestApi() { |
|
|
|
|
return new Docket(DocumentationType.SWAGGER_2) |
|
|
|
|
.apiInfo(apiInfo()) |
|
|
|
|
.select() |
|
|
|
|
//为当前包路径,控制器类包
|
|
|
|
|
.apis(RequestHandlerSelectors.basePackage("cc.bnblogs.swagger_study.controller")) |
|
|
|
|
.paths(PathSelectors.any()) |
|
|
|
|
public Docket docket(){ |
|
|
|
|
// 新建一个全局参数
|
|
|
|
|
Parameter token = new ParameterBuilder().name("token") |
|
|
|
|
// 描述信息
|
|
|
|
|
.description("用户登录令牌") |
|
|
|
|
// 该参数放在请求头
|
|
|
|
|
.parameterType("header") // 也可以设置query参数,相当于@RequestParam
|
|
|
|
|
// 参数的类型
|
|
|
|
|
.modelRef(new ModelRef("String")) |
|
|
|
|
// 该参数必填
|
|
|
|
|
.required(true) |
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
List<Parameter> parameters = new ArrayList<>(); |
|
|
|
|
parameters.add(token); |
|
|
|
|
|
|
|
|
|
return new Docket(DocumentationType.SWAGGER_2) |
|
|
|
|
.globalOperationParameters(parameters).apiInfo(apiInfo()); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// @Bean
|
|
|
|
|
// public Docket RestfulApi() {
|
|
|
|
|
// return new Docket(DocumentationType.SWAGGER_2)
|
|
|
|
|
// .apiInfo(apiInfo())
|
|
|
|
|
// .select()
|
|
|
|
|
// .apis(RequestHandlerSelectors.basePackage("cc.bnblogs.swagger_study.controller"))
|
|
|
|
|
// .paths(PathSelectors.any()).build();
|
|
|
|
|
// }
|
|
|
|
|
private ApiInfo apiInfo() { |
|
|
|
|
return new ApiInfoBuilder() |
|
|
|
|
//页面标题
|
|
|
|
@ -41,7 +69,7 @@ public class SwaggerConfig { |
|
|
|
|
//版本号
|
|
|
|
|
.version("1.0") |
|
|
|
|
//描述
|
|
|
|
|
.description("演示系统API描述") |
|
|
|
|
.description("学习使用Swagger") |
|
|
|
|
.build(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|