- 首先添加依赖包
<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>
- 编写Swagger2的配置类
@EnableSwagger2
@EnableWebMvc
public class Swagger2Config {
@Bean
public Docket buidDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.**.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口文档")
.description("api接口文档")
.version("1.0.0")
.termsOfServiceUrl("")
.build();
}
}
- 在SpringMvc的配置文件中配置
<bean class="com.**.Swagger2Config"/>
注意:如果还集成了shiro框架,那需要在shiro的拦截配置中放行以下路径:
/swagger-ui.html = anon
/webjars/** = anon
/v2/** = anon
/swagger-resources/** = anon