项目源码:http://download.csdn.net/download/a295277302/9943725
添加jar依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
之后添加类 SwaggerConfig
@Configuration @EnableSwagger2 @ComponentScan("com.htf.controller") public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.htf.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { ApiInfo apiInfo = new ApiInfoBuilder().title("Spring Boot Sample REST APIs") .description("The APIs here demonstrate creating a service built with Spring Boot") .license("MIT") .licenseUrl("http://opensource.org/licenses/MIT") .contact("htf") .version("1.0") .build(); return apiInfo; } }
如果整合了Shiro/spring security
添加网页访问过滤的话 必须要将
/v2/api-docs,/swagger-resources这两个给过滤掉,
![]()
spring oauth2
![]()
以Shiro为例
//开放swagger资源 start filterMap.put("/v2/api-docs", "anon"); filterMap.put("/webjars/**", "anon"); filterMap.put("/swagger-resources/**", "anon"); filterMap.put("/swagger-ui.html", "anon"); //开放swagger资源 end