swagger3

使用过程

引包

  swagger3升级后,引入的包做相应升级,且集成进springboot启动器内,这里直接引入即可。

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

配置

  这里配置与swagger2发生了些许变化:

启动类

  启动类中开启swagger的注解变为** @EnableOpenApi**。

1
2
3
4
5
6
7
8
@EnableOpenApi
public class EbwcrmApplication {

public static void main(String[] args) {
SpringApplication.run(EbwcrmApplication.class, args);
}

}

配置类

  配置类中Docket类型需要修改为** DocumentationType.OAS_30**。

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
@Configuration
@ConditionalOnProperty(value = {"ebwcrm.api.enable-swagger"}, havingValue = "true")
public class SwaggerConfig {

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any()).build();
}

/**
* 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("集盈客Api接口文档")
// 创建人
.contact(new Contact("linqiankun", "http://localhost:8008/ebwcrm/swagger-ui/index.html", "linqiankun_51309@QQ.com"))
// 版本号
.version("1.0")
// 描述
.description("根据服务端接口规范设计:http://doc.fed.weidai.work/docs/web/api").build();
}
}

  拦截器中需要对swagger的静态资源进行放行,以免启动后无法访问到swagger。

1
2
3
4
5
6
7
8
9
10
11
12
@Override
public void addInterceptors(InterceptorRegistry registry) {
List<String> pathPattern = new ArrayList<>();
pathPattern.add("/**");
List<String> excludePattern = new ArrayList<>();
excludePattern.add("/swagger-ui/**");
excludePattern.add("/swagger-resources/**");
excludePattern.add("/v3/api-docs");
registry.addInterceptor(loginInterceptor)
.addPathPatterns(pathPattern)
.excludePathPatterns(excludePattern);
}

使用

  接口使用的注解,与swagger2基本相同。

1
2
3
4
5
6
7
8
9
10
11
@RestController
@RequestMapping("/miUser")
@Api(tags = "Controller")
public class MiUserController {

@ApiOperation("updatePassword")
@PostMapping("updatePassword")
public EBwcrmResult updatePassword() {
}
}

  swagger3相对于swagger2的文档路径发生了改变。

1
http://<ip>:<port>/<applicattion>/swagger-ui/index.html