依賴:
<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構(gòu)建API文檔")
? ? ? ? ? ? ? ? .description("使用RestFul風(fēng)格, 創(chuàng)建人:longma4")
? ? ? ? ? ? ? ? .termsOfServiceUrl("https://github.com/cicadasmile")
? ? ? ? ? ? ? ? .version("version 1.0")
? ? ? ? ? ? ? ? .build();
? ? }
}
啟動類注解:
@SpringBootApplication
@MapperScan("com.example.dao")
@EnableSwagger2
public class SampleWebJspApplicationextends SpringBootServletInitializer{
? *********
}
注意:這里我的swagger配置文件和啟動類在同一個目錄才可以际度。