knife4j只用此插件的最簡潔開發方式


一、POM添加

在pom文件里添加包

1 <!--引入knife4j以來--> 
2 <dependency> 
3 <groupId>com.github.xiaoymin</groupId> 
4 <artifactId>knife4j-spring-boot-starter</artifactId> <version>1.9.6</version> 
5 </dependency>
View Code

二、配置添加,相當於<bean>添加

 1 package com.mrliu.undertow.conf;
 2 
 3 import com.github.xiaoymin.knife4j.spring.annotations.EnableSwaggerBootstrapUi;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import springfox.documentation.builders.ApiInfoBuilder;
 7 import springfox.documentation.builders.PathSelectors;
 8 import springfox.documentation.builders.RequestHandlerSelectors;
 9 import springfox.documentation.service.ApiInfo;
10 import springfox.documentation.service.Contact;
11 import springfox.documentation.spi.DocumentationType;
12 import springfox.documentation.spring.web.plugins.Docket;
13 import springfox.documentation.swagger2.annotations.EnableSwagger2;
14 
15 /**
16  * @author liuyang
17  */
18 @Configuration
19 @EnableSwagger2
20 @EnableSwaggerBootstrapUi
21 public class Swagger2Config {
22 
23     /**
24      * 創建連接的包信息
25      * <p>
26      * 配置統一返回的controller路徑RequestHandlerSelectors.basePackage
27      *
28      * @return 返回創建狀況
29      */
30     @Bean
31     public Docket createRestApi() {
32         return new Docket(DocumentationType.SWAGGER_2)
33                 .useDefaultResponseMessages(false)
34                 .apiInfo(apiInfo())
35                 .select()
36                 .apis(RequestHandlerSelectors.basePackage("com.mrliu.undertow.controller"))
37                 .paths(PathSelectors.any())
38                 .build();
39 
40     }
41 
42 
43     /**
44      * 設置文檔信息主頁的內容說明
45      *
46      * @return 文檔信息
47      */
48     private ApiInfo apiInfo() {
49         return new ApiInfoBuilder()
50                 .title("Project textBook API ")
51                 .description("服務接口")
52                 .termsOfServiceUrl("http://localhost:8001/")
53                 .contact(new Contact("Mr Liu", "http://localhost:8999/", "liuyang@synway.cn"))
54                 .license("what")
55                 .version("1.0")
56                 .build();
57     }
58 
59 }
View Code

apiInfo()設置后會改變如圖

三、bean實體類添加

 1 /**
 2  * @author Administrator
 3  */
 4 @ApiModel("用戶對象")
 5 @Data
 6 public class UserVO {
 7 
 8     @ApiModelProperty(required = true, notes = "用戶名", example = "blues")
 9     private String name;
10 
11     @ApiModelProperty(required = true, notes = "用戶返回消息", example = "hello world")
12     private String words;
13 
14 
15     public UserVO(String name, String words) {
16         this.name = name;
17         this.words = words;
18     }
19 }
View Code

主要是添加注解

@ApiModel("用戶對象") -----實體類注解添加 添加后結果如圖所示:

@ApiModelProperty(required = true, notes = "用戶名", example = "blues") 字段注解添加 添加后結果如圖所示:

四、Controller添加

  • 1、GET方式訪問
     1 package com.mrliu.undertow.controller;
     2 
     3 import com.mrliu.undertow.base.Results;
     4 import com.mrliu.undertow.pojo.UserVO;
     5 import io.swagger.annotations.*;
     6 import org.springframework.web.bind.annotation.GetMapping;
     7 import org.springframework.web.bind.annotation.PathVariable;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 import org.springframework.web.bind.annotation.RestController;
    10 
    11 import javax.servlet.http.HttpServletRequest;
    12 import javax.servlet.http.HttpServletResponse;
    13 import java.io.IOException;
    14 
    15 /**
    16  * @author Administrator
    17  */
    18 @Api(tags = "HELLO CONTROLLER 測試功能接口")
    19 @RestController
    20 public class HelloController {
    21 
    22 
    23     @ApiImplicitParams({
    24             @ApiImplicitParam(name = "name",value = "用戶名稱",required = true,dataType = "String",paramType = "path",example = "blues")
    25     })
    26     @ApiResponses(value = {
    27             @ApiResponse(code = 200, message = "接口返回成功狀態"),
    28             @ApiResponse(code = 500, message = "接口返回未知錯誤,請聯系開發人員調試")
    29     })
    30     @ApiOperation(value = "Hello 測試接口", notes = "訪問此接口,返回hello語句,測試接口")
    31     @GetMapping("hello/{name}")
    32     public Results<UserVO> hello(@PathVariable String name){
    33         UserVO userVO = new UserVO(name,"hello " + name);
    34         Results<UserVO> results = new Results<>(200,"SUCCESS", userVO);
    35         return results;
    36     }
    37 }
    View Code
     界面生成:

  • 2、POST方式訪問
 1 @Api(tags = "HELLO CONTROLLER 測試功能接口")
 2 @RestController
 3 public class HelloController {
 4 
 5 
 6     @ApiImplicitParams({
 7             @ApiImplicitParam(name = "name",value = "用戶名稱",required = true,dataType = "String",paramType = "path",example = "blues")
 8     })
 9     @ApiResponses(value = {
10             @ApiResponse(code = 200, message = "接口返回成功狀態"),
11             @ApiResponse(code = 500, message = "接口返回未知錯誤,請聯系開發人員調試")
12     })
13     @ApiOperation(value = "Hello 測試接口", notes = "訪問此接口,返回hello語句,測試接口")
14     @PostMapping("hello/{name}")
15     public Results<UserVO> hello(@RequestBody UserVO userVO){
16         Results<UserVO> results = new Results<>(200,"SUCCESS", userVO);
17         return results;
18     }
19 }
View Code

五、訪問URL

http://localhost:7788/doc.html

六、兼容swagger-ui訪問

http://localhost:7788/swagger-ui.html

七、測試

填入參數

對象請求訪問

八、API文檔復制

復制后可生成markdow文檔,使用showdoc,即可翻譯成文檔,下載html、PDF、word等格式


源碼地址:https://github.com/liushaoye/knife4j.git  歡迎點贊,分享,推薦


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM