项目开发中需要写一些api开发文档,如果不写的话容易忘记这个接口的请求方法以及参数等。本期利用swagger生成实时api文档
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
@Configuration
@EnableSwagger2
public class SwaggerConfig {
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("online")
.description("在线视频api接口文档")
.termsOfServiceUrl("https://tanblog.cc")
.version("1.0")
.build();
}
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cc.tanblog.online"))
.paths(PathSelectors.regex(".*"))
.build();
}
}
WebConfiguration
/**
* 配置拦截器、拦截路径
* 每次请求到拦截的路径,就会去执行拦截器中的方法
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
List<String> excludePath = new ArrayList<>();
//排除拦截,除了注册登录(此时还没token),其他都拦截
excludePath.add("/swagger-ui.html");
excludePath.add("/swagger-resources/**");
excludePath.add("/v2/**");
excludePath.add("/doc.html/**");
excludePath.add("/webjars/**");
excludePath.add("/usercontroller/login"); //登录
registry.addInterceptor(tokenInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(excludePath);
WebMvcConfigurer.super.addInterceptors(registry);
}
浏览器访问:http://127.0.0.1:8080/swagger-ui.html 效果图如下