knife4j的配置过程如下:
1、首先引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
2、创建一个spring boot模块然后修改模块的结构如下图:
3、spring.factories中的内容为:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jjw.core.knif4j.MyKnife4jAutoConfiguration
4、编写配置类,结构如下图:
5、MyKnife4jAutoConfiguration中的内容如下:
package com.jjw.core.knif4j;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.HashSet;
/**
* // 使用需要在其yml中添加如下配置 可以看 service-admin中如何配置的
* //steven:
* // knife4j:
* // basePackage: com.jjw.admin.controller
* // title: 哈哈哈我的title
*
*
*
*/
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@EnableConfigurationProperties(MyKnife4jProperties.class)
public class MyKnife4jAutoConfiguration {
/**
* 这种是用到了需要在service加入此依赖才可以使用
* 另外需要在yml中设置title
*/
// 使用需要在其yml中添加如下配置 可以看 service-admin中如何配置的
//steven:
// knife4j:
// basePackage: com.jjw.admin.controller
// title: 哈哈哈我的title
@Autowired
private MyKnife4jProperties myKnife4jProperties;
@Bean
@ConditionalOnMissingBean(Docket.class)//干嘛:当 其他人没有配置docket的对象交给spring容器管理的时候。我这个方法就会执行
public Docket defaultApi2() {
HashSet<String> strings = new HashSet<>();
strings.add("application/json");
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title(myKnife4jProperties.getTitle())
.description(myKnife4jProperties.getDescription())
.termsOfServiceUrl(myKnife4jProperties.getServiceUrl())
.contact(myKnife4jProperties.getContact())
.version(myKnife4jProperties.getVersion())
.build())
.produces(strings)
//分组名称
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage(myKnife4jProperties.getBasePackage()))
.paths(PathSelectors.any())
.build();
return docket;
}
}
6、myKnife4jProperties中的内容如下:
package com.jjw.core.knif4j;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("steven.knife4j")
@Data
public class MyKnife4jProperties {
private String basePackage = "com.jjw";
private String description = "默认描述";
private String title = "默认标题";
private String contact = "联系人";
private String version = "1.0";
private String serviceUrl = "http://www.jjw.com";
}
使用:在其他微服务中引入此依赖
<dependency>
<groupId>com.jjw</groupId>
<artifactId>new-swagger</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
运行微服务后即可在浏览器上使用微服务的ip:端口/doc.html查看接口文档
例如:http://localhost:8002/doc.html