1.当然,首先是创建一个Spring Boot项目,加入web依赖,创建成功后,加入两个Swagger2相关的依赖,完整的依赖如下:<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.Swagger2的配置也是比较容易的,在项目创建成功之后,只需要开发者自己提供一个Docket的Bean即可,如下:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/") .select() .apis(RequestHandlerSelectors.basePackage("com.xx.controller")) .paths(PathSelectors.any()) .build().apiInfo(new ApiInfoBuilder() .title("sois") .description("sois") .version("1.0") .contact(new Contact("xx","xx","xx@gmail.com")) .license("The Apache License") .licenseUrl("http://www.xx.com") .build()); } }
3.此时启动项目,输入http://localhost:8080/swagger-ui.html,能够看到如下页面,说明已经配置成功了:
问题一 guava的版本和swagger的版本不一致需升级guava的版本。
问题二:一直弹框。开始使用的是springfox-swagger2是的2.9.2版本。后面降级为2.7.0版本。
问题三,如果有cas校验需要对相关的url进行放行。
/swagger-ui.html|/webjars/**|/api/v2/api-docs/*|/swagger-resources/**|/v2/**