第一步:在maven项目的pom.xml中引入Knife4j的依赖包,代码如下:
com.github.xiaoymin knife4j-spring-boot-starter 2.0.7
第二步:创建Swagger配置依赖,代码如下:
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket=new Docket(documentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
//.title("swagger-bootstrap-ui-demo RESTful APIs")
.description("# swagger-bootstrap-ui-demo RESTful APIs")
.termsOfServiceUrl("http://www.xx.com/")
.contact(new Contact("name","url","email"))
.version("1.0")
.build())
//配置token 或 请求头 Authorization
.securitySchemes(securitySchemes())
//分组名称
.groupName("2.X版本")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.it.knife4j.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
//配置请求头的方法
private List securitySchemes()
{
List apiKeyList = new ArrayList();
apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue()));
return apiKeyList;
}
}
第三步: 给controller配置注解
@Api(tags = "知识库模块")
@RestController
public class UserController {
@ApiOperation(value = "新增知识库")
@ApiImplicitParams({
@ApiImplicitParam(name = "id",value = "id",required = true,dataType ="String"),
@ApiImplicitParam(name = "deptID",value = "部门id",dataType ="Long")
}
)
@DeleteMapping("/hello")
public String hello(String id, String deptID ){
return "hello";
}
}
第四步: 访问地址http://ip:port/doc.html 配置token
会为每个接口添加token
第五步: 点击发送请求



