添加依赖
在pom文件中加入knife4j
最新版本maven依赖
1 2 3 4 5
| <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
|
如果还需要进入swagger-ui接口文档页面需要加入与kinfe4j所包含swagger对应版本的swagger-ui依赖(为什么加这个在文末说明)
1 2 3 4 5
| <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency>
|
配置类
新建一个通用项目属性配置类和一个swagger配置类
ProjectConfig配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties; import com.alibaba.nacos.api.config.annotation.NacosProperty; import com.alibaba.nacos.api.config.annotation.NacosValue; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Configuration;
@Data @Configuration public class ProjectConfig { @Value("${spring.application.name}") private String applicationName;
@Value("${configs.product-desc}") private String productDesc;
@Value("${server.port}") private String serverPort;
@Value("${server.servlet.context-path:}") private String contextPath; }
|
swagger配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.annotation.Order; import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2;
import javax.annotation.Resource;
@RefreshScope @Configuration @EnableSwagger2 @EnableKnife4j @Import(BeanValidatorPluginsConfiguration.class) @ConditionalOnExpression("${knife4j.enable}") public class SwaggerConfig {
@Resource ProjectConfig projectConfig;
@Bean(value = "userApi") @Order(value = 1) public Docket groupRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .enable(true) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); }
private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(projectConfig.getApplicationName() + "接口文档") .description(projectConfig.getProductDesc()) .termsOfServiceUrl("http://localhost:"+projectConfig.getServerPort())
.version("2.0").build(); }
}
|
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| configs: product-desc: xx服务47 swagger: service: base: package: com.*.controller
knife4j: basic: username: admin password: 123456
enable: true
production: false
enable: true
|
此时已经可以通过项目访问根路径+doc.html 访问到Knife4j的接口文档页面了。
之所以要加入 swagger-ui
maven依赖,这是因为Knife4j中虽然引入了swagger的大部分核心依赖但是没有引入这个前端依赖,如果不加入这个依赖,swagger页面是访问不到的。