- 在
common模块中添加SwaggerUI3.0依赖,parent项目中已经声明了版本
<!--swagger ui接口文档依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
- 创建SwaggerConfiguration配置类
@Component
@EnableOpenApi
@Data
public class SwaggerConfiguration {
@Bean
public Docket webApiDoc() {
return new Docket(DocumentationType.OAS_30)
.groupName("用户微服务接口文档")
.pathMapping("/")
//定义是否开启swagger, false为关闭
.enable(true)
//配置api文档元信息
.apiInfo(apiInfo())
//选择那些接口作为swagger的doc发布
.select()
.apis(RequestHandlerSelectors.basePackage("com.guslegend"))
//正则匹配请求路径, 并分配到当前组
.paths(PathSelectors.ant("/api/**"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("1024电商平台")
.description("微服务接口文档")
.contact(new Contact("guslegend","http://guslegend.com","1670547022@qq.com"))
.version("12")
.build();
}
}
@Bean
public Docket adminApiDoc(){
return new Docket(DocumentationType.OAS_30)
.groupName("管理端接口文档")
.pathMapping("/")
// 定义是否开启swagger,false为关闭,可以通过变量控制,线上关闭
.enable(true)
//配置api文档元信息
.apiInfo(apiInfo())
// 选择哪些接口作为swagger的doc发布
.select()
.apis(RequestHandlerSelectors.basePackage("com.guslegend"))
//正则匹配请求路径,并分配至当前分组
.paths(PathSelectors.ant("/admin/**"))
.build();
}
package com.guslegend.config;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.List;
@Component
@EnableOpenApi
@Data
public class SwaggerConfiguration {
@Bean
public Docket webApiDoc() {
return new Docket(DocumentationType.OAS_30)
.groupName("用户微服务接口文档")
.pathMapping("/")
//定义是否开启swagger, false为关闭
.enable(true)
//配置api文档元信息
.apiInfo(apiInfo())
//选择那些接口作为swagger的doc发布
.select()
.apis(RequestHandlerSelectors.basePackage("com.guslegend"))
//正则匹配请求路径, 并分配到当前组
.paths(PathSelectors.ant("/api/**"))
.build()
//新版swagger3.0配置
.globalRequestParameters(getGlobalRequestParameters())
.globalResponses(HttpMethod.GET, getGlobalResponseMessage())
.globalResponses(HttpMethod.POST, getGlobalResponseMessage());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("1024电商平台")
.description("微服务接口文档")
.contact(new Contact("guslegend","http://guslegend.com","1670547022@qq.com"))
.version("12")
.build();
}
/**
* 生成全局通用参数, 支持配置多个响应参数
* @return
*/
private List<RequestParameter> getGlobalRequestParameters() {
List<RequestParameter> parameters = new ArrayList<>();
parameters.add(new RequestParameterBuilder()
.name("token")
.description("登录令牌")
.in(ParameterType.HEADER)
.query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
.required(false)
.build());
// parameters.add(new RequestParameterBuilder()
// .name("version")
// .description("版本号")
// .required(true)
// .in(ParameterType.HEADER)
// .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
// .required(false)
// .build());
return parameters;
}
/**
* 生成通用响应信息
* @return
*/
private List<Response> getGlobalResponseMessage() {
List<Response> responseList = new ArrayList<>();
responseList.add(new ResponseBuilder().code("4xx").description("请求错误,根据code和msg检查").build());
return responseList;
}
}