swagger使用
1、swagger作用
前后端分离,需要说明接口定义内容。
2、引入依赖
2.9.2
2.9.2
1.9.0
io.springfox
springfox-swagger2
${springfox-swagger2.version}
io.springfox
springfox-swagger-ui
${springfox-swagger2.version}
com.github.xiaoymin
swagger-bootstrap-ui
${swagger-bootstrap-ui.version}
3、swagger配置
@Configuration
@EnableSwagger2
@ComponentScan
@Slf4j
public class SwaggerConfig {
private String title = "保险API模拟";
private String desc="保险API模拟";
private String ver="1.0";
private String email="WWW";
private String allowGenerate="true";
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(title)
.description(desc)
.contact(email)
.version(ver)
.build();
}
@Bean
public Docket swaggerSpringMvcPlugin() {
Docket docket = null;
// 可以根据配置决定不做任何API生成
if ("false".equals(allowGenerate)) {
docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select().apis(RequestHandlerSelectors.none()).build();
} else {
docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 标示只有被 @Api 标注的才能生成API.
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build();
}
return docket;
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("保险API")
.select()
.apis(RequestHandlerSelectors.basePackage("cn.com.kxb.web.controller"))
.paths(PathSelectors.any())
.build();
}
}