使用swaggerui

:::warning

不推荐使用

:::

swaggerui配置

添加swaggerui依赖

1
2
3
4
5
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

在主类添加注解: @EnableOpenApi

最后添加配置类和拦截器类

配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

@Configuration
@EnableOpenApi
//@ComponentScan("com.qgzx.mapper")
public class SwaggerConfig {
@Bean
public Docket docker() {
// 构造函数传入初始化规范,这是swagger2规范
return new Docket(DocumentationType.OAS_30)
//apiInfo: 添加api详情信息,参数为ApiInfo类型的参数,这个参数包含了第二部分的所有信息比如标题、描述、版本之类的,开发中一般都会自定义这些信息
.apiInfo(apiInfo())
.groupName("group")
//配置是否启用Swagger,如果是false,在浏览器将无法访问,默认是true
.enable(true)
.select()
//apis: 添加过滤条件,
.apis(RequestHandlerSelectors.basePackage("cn.hellohao.controller"))
//paths: 这里是控制哪些路径的api会被显示出来,比如下方的参数就是除了/user以外的其它路径都会生成api文档

.build();
}

private ApiInfo apiInfo() {
Contact contact = new Contact("名字:name", "个人链接:http://xxx.xxx.com/", "邮箱:XXX");
return new ApiInfo(
"标题内容",
"描述内容",
"版本内容:v1.0",
"链接:http://terms.service.url/",
contact,
"许可:Apach 2.0 ",
"许可链接:XXX",
new ArrayList<>()
);
}
}

拦截器类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@Component
public class SwaggerUiWebMvcConfigurer implements WebMvcConfigurer {
private final String baseUrl;

public SwaggerUiWebMvcConfigurer(
@Value("${springfox.documentation.swagger-ui.base-url:}") String baseUrl) {
this.baseUrl = baseUrl;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');

registry.
addResourceHandler(baseUrl + "/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController(baseUrl + "/swagger-ui/")
.setViewName("forward:" + baseUrl + "/swagger-ui/index.html");
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/api/pet")
.allowedOrigins("http://editor.swagger.io");
registry
.addMapping("/v2/api-docs.*")
.allowedOrigins("http://editor.swagger.io");
}
}

配置token

待续…

Buy me a coffee
静听、唯美殇 微信 微信
静听、唯美殇 支付宝 支付宝