依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
配置文件:
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfigimplements WebMvcConfigurer{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public DocketcreateRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//此包路径下的类,才生成接口文档
.apis(RequestHandlerSelectors.basePackage("com.example"))
//加了ApiOperation注解的类,才生成接口文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
//.globalOperationParameters(setHeaderToken());
}
private ApiInfoapiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot利用Swagger构建API文档")
.description("使用RestFul风格, 创建人:longma4")
.termsOfServiceUrl("https://github.com/cicadasmile")
.version("version 1.0")
.build();
}
}
启动类注解:
@SpringBootApplication
@MapperScan("com.example.dao")
@EnableSwagger2
public class SampleWebJspApplicationextends SpringBootServletInitializer{
*********
}
注意:这里我的swagger配置文件和启动类在同一个目录才可以。