一、添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
二、创建SwaggerWebMvcConfig.java
@Configuration
public class SwaggerWebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
三、创建SwaggerConfig
@Configuration
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("业务接口")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.yc.practice.mall"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket createSystemRestApi() {
Predicate<RequestHandler> selector1 = RequestHandlerSelectors.basePackage("com.yc.practice.system.controller");
return new Docket(DocumentationType.SWAGGER_2)
.groupName("系统接口")
.apiInfo(apiInfo())
.select()
.apis(Predicates.or(selector1))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口文档")
.description("Restful风格接口文档")
.version("1.0")
.build();
}
}
四、security配置类中过滤swagger相关信息
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers( "/swagger-ui.html")
.antMatchers("/webjars/**")
.antMatchers("/v2/**")
.antMatchers("/swagger-resources/**");
}
五、访问http://ip:port/ swagger-ui.html即可