在pom.xml添加依赖
<!--swagger依赖-->
<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配置类
package com.study.swagger2;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
@Configuration//标注该类是个配置类
@EnableSwagger2//启用Swagger2
public class SwaggerConfig {
private boolean enableSwagger;//是否启用Swagger
public SwaggerConfig() {
this.enableSwagger = true;
}
@Bean
public Docket createRestApi() {
//添加head请求头参数 start
ParameterBuilder ticketPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
ticketPar.name("token").description("token")//name表示名称,description表示描述
.modelRef(new ModelRef("string")).parameterType("header")
.required(false).defaultValue("").build();//required表示是否必填,defaultValue表示默认值
pars.add(ticketPar.build());
//end
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//设置文档信息
.enable(enableSwagger)
.select()//函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
//此包路径下的类,才生成接口文档
.apis(RequestHandlerSelectors.basePackage("com.study"))//这里采用包扫描的方式来确定要显示的接口
//加了ApiOperation注解的方法,才生成接口文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//这里采用包含注解的方式来确定要显示的接口
.paths(PathSelectors.any())//返回包含所有满足条件的请求处理器的断言,该断言总为true
.build()
.globalOperationParameters(pars);//消息头添加
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("公共服务接口 APIs")//页面标题
.description("")//描述
.version("0.1.1")//版本
.build();
}
}
在启动项目后,swagger-ui.html显示如下图
解决方法:
1,在swagger配置类添加@EnableWebMvc注解
@EnableWebMvc//开启对Web MVC 的配置支持
@Configuration//标注该类是个配置类
@EnableSwagger2//启用Swagger2
public class SwaggerConfig {
2,在spring配置文件添加 <context:component-scan/>扫描swagger配置类
<!-- 扫描swagger配置文件 -->
<context:component-scan base-package="com.study.swagger2"/>
再次启动项目后,显示如下图(别忘了在Controller类的方法上添加@ApiOperation注解)
不显示API接口解决方法:
在swagger配置类添加@ComponentScan注解用来扫描接口所在包的位置
@ComponentScan("com.study")//需要扫描的api位置
@EnableWebMvc//开启对Web MVC 的配置支持
@Configuration//标注该类是个配置类
@EnableSwagger2//启用Swagger2
public class SwaggerConfig {
再次启动项目后显示如下图成功