文章目录
SpringBoot集成Swagger
1、建立一个SpringBoot项目
2、导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3、编写swagger配置类
@Configuration
@EnableSwagger2//开启Swagger2的自动配置
public class SwaggerConfig {
}
4、打开网页http://localhost:8080/swagger-ui.html,可以看见swagger网页则成功。
配置Swagger
1、通过配置Swagger的实例Docket来配置Swagger
//Swagger实例Bean是Docket,所以通过配置Docket实例来配置Swagger。
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
2、配置文档信息
//配置Swagger信息
private ApiInfo apiInfo(){
Contact contact = new Contact("xhj", "www.xxx.com", "xxxxx.qq.com");
return new ApiInfo( "title","description","version","termsOfServiceUrl",contact,"license","licenseUrl",new ArrayList());
}
由于源码中只有一个构造方法,没有set方法,所以只能通过构造方法配置信息。
Swagger配置扫描接口
1、通过select方法扫描接口
@Bean
public Docket docket() {
//any扫描项目中的所有接口
//none不扫描接口
//withMethodAnnotation(final Class<? extends Annotation> annotation),通过类上的注解扫描,如.withClassAnnotation(Controller.class)只扫描有 controller注解的类中的接口
//basePackage(final String basePackage),根据包路径扫描接口
//regex(final String pathRegex),通过正则表达式控制
//ant(final String antPattern),通过ant(