Knife4j is an enhanced solution that generates API documentation for Swagger integration. It separates front-end and back-end Java code and front-end Ui modules to be used in a more flexible microservice architecture. Knife4j provides an enhanced solution that focuses on Swagger, rather than just improving and enhancing front-end Ui parts. Instead of Swagger-UI, we use Knife4J as a document management tool.
> > Gitegg-platform > > Gitegg-platform > > Gitegg-platform > > Gitegg-platform > > > Pom.xml > > > < gitegg-platform > > < gitegg-platform > > Using Knife4J as Maven BOM:
<! <dependency> <groupId>com.github. Xiaoymin </groupId> <artifactId>knife4j-dependencies</artifactId> <version>${knife4j.version}</version> <type>pom</type> <scope>import</scope> </dependency>Copy the code
2, Add knife4j reference to POM. XML in gitegg-platform-Swagger subproject
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < the parent > < artifactId > GitEgg - Platform < / artifactId > < groupId > com. GitEgg. Platform < / groupId > < version > 1.0 - the SNAPSHOT < / version > < / parent > < modelVersion > 4.0.0 < / modelVersion > < artifactId > gitegg - platform - swagger < / artifactId > <name>${project.artifactId}</name> <version>${project.parent.version}</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> </dependency> </dependencies> </project>Copy the code
> gitegg-platform-swagger > gitegg-platform-swagger > gitegg-platform-swagger > gitegg-platform-swagger > gitegg-platform-swagger >
package com.gitegg.platform.swagger.config; import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; 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.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 @EnableKnife4j @Import(BeanValidatorPluginsConfiguration.class) public class SwaggerConfig { @Bean(value = "GitEggApi") public Docket GitEggApi() { Docket docket=new Docket (DocumentationType SWAGGER_2). ApiInfo (apiInfo ()) / / group name. The groupName (" 2 X version "). The select () / / Controller scan packages specified path here .apis(RequestHandlerSelectors.basePackage("com.gitegg.*.*.controller")) .paths(PathSelectors.any()) .build(); return docket; } private ApiInfo ApiInfo () {return new ApiInfoBuilder().version("1.0.0").title("Spring Cloud Swagger2 ") .description("Spring Cloud Swagger2 document ").termsofServiceurl ("www.gitegg.com").build(); }}Copy the code
4, introduce gitegg-platform-swagger in gitegg-service project
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < the parent > < artifactId > GitEgg -- Cloud < / artifactId > < groupId > com. GitEgg. Cloud < / groupId > < version > 1.0 - the SNAPSHOT < / version > < / parent > The < modelVersion > 4.0.0 < / modelVersion > < artifactId > gitegg - service < / artifactId > < packaging > pom < / packaging > < modules > <module>gitegg-service-base</module> <module>gitegg-service-bigdata</module> <module>gitegg-service-system</module> </modules> <dependencies> <! Gitegg --> <dependency> <groupId>com.gitegg.platform</groupId> <artifactId> </dependency> <! -- gitegg mybatis-plus --> <dependency> <groupId>com.gitegg.platform</groupId> <artifactId>gitegg-platform-mybatis</artifactId> </dependency> <! -- gitegg swagger2-knife4j --> <dependency> <groupId>com.gitegg.platform</groupId> <artifactId>gitegg-platform-swagger</artifactId> </dependency> <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>Copy the code
Add Swagger2 annotations to the systemController.java class under gitegg-service-system project
package com.gitegg.service.system.controller; import com.gitegg.service.system.service.ISystemService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "system") @AllArgsConstructor @Api(tags = "gitegg-system") public class SystemController { private final ISystemService systemService; @getMapping (value = "list") @apiOperation (value = "system List ") public Object list() {return SystemService.list (); } @getMapping (value = "page") @apiOperation (value = "system Page ") public Object Page () {return SystemService.page ();} @getMapping (value = "page") @apiOperation (value = "system Page ") public Object Page () {return systemService.page(); }}Copy the code
6, GitEggSystemApplication. Java to join component scanning annotations, let the Spring loaded at startup to swagger2 configuration:
package com.gitegg.service.system; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; /** * ComponentScan(basePackages = "com.gitegg") @mapperscan ("com.gitegg.*.*.mapper") @SpringBootApplication public class GitEggSystemApplication { public static void main(String[] args) { SpringApplication.run(GitEggSystemApplication.class,args); }}Copy the code
7, run gitegg – service – system, open a browser visit: http://127.0.0.1:8001/doc.html, you can see swagger2 document interface
The source of this article is available at gitee.com/wmz1930/Git… Chapter -06 of the