swagger使用
1、swagger作用
前后端分离,需要说明接口定义内容。
2、引入依赖
<properties>
<springfox-swagger2.version>2.9.2</springfox-swagger2.version>
<springfox-swagger-ui.version>2.9.2</springfox-swagger-ui.version>
<swagger-bootstrap-ui.version>1.9.0</swagger-bootstrap-ui.version>
</properties>
<!--swagger2 start-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger2.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${swagger-bootstrap-ui.version}</version>
</dependency>
<!--swagger2 end-->
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();
}
}