1.依赖
<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>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.1.0</version>
</dependency>
注意事项:spring 的版本要跟swagger版本对应否则会出问题
此时项目spring版本为4.0.6.RELEASE
配置类:
/**
* Swagger2配置类
* 在与spring boot集成时,放在与Application.java同级的目录下。
* 通过@Configuration注解,让Spring来加载该类配置。
* 再通过@EnableSwagger2注解来启用Swagger2。
*/
@Configuration
@EnableWebMvc
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.wms.pms.web.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SwaggerAPI")
// .termsOfServiceUrl("127.0.0.1:8000")
.version("1.0")
.build();
}
}
配置文件:
<!-- swagger -->
<!--这个地方是配置类的路径-->
<bean class="com.wms.pms.common.config.Swagger2Config"></bean>
<mvc:default-servlet-handler/>
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
上面步骤集成完毕之后就可以正常使用swagger的注解了
集成过程中出现如下错误
第一个错误
org.fasterxml.classmate ResolvedParameterizedMember
原因是下面的这个依赖必须要1.1.0以上
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.1.0</version>
</dependency>
第二个错误 是一个弹窗错误
此时怀疑是下面这个依赖的问题之前此依赖是2.5.0以上的版本,手动修改为2.5.0以后再次访问,问题解决
下面这个依赖如果不添加会导致
http://127.0.0.1:8000/swagger-ui.html
404
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
遗留问题
在另外一台电脑做springmvc集成swagger2时只添加以下依赖可以正常流程进行访问swagger页面
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>