一、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>

二、配置添加,相当于<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 }

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 }

主要是添加注解

@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 }

    界面生成:

  • 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 }

五、访问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  欢迎点赞,分享,推荐

knife4j只用此插件的最简洁开发方式的更多相关文章

  1. 【eclipse插件开发实战】Eclipse插件开发4——插件JDE、PDE开发方式及plugin.xml配置文件结构

    Eclipse插件开发4--插件JDE.PDE开发方式及plugin.xml配置文件结构 开发方式分为:java开发环境JDE开发插件的方式和插件开发环境PDE开发插件方式. 插件通过添加到预定义的扩 ...

  2. 2022IDEA配置启动lilishop的swagger展示

    目录 一.概述 二.基本构建 三.Git 导入编译器 四.模块描述浅析 五.配置文档 1.注释配置文件 2.添加配置 3.暂时关闭权限 4.浏览器测试访问 5.其他需要修改模块 六.参考文献 结语 一 ...

  3. Xcode插件优缺点对比(推荐20款插件)

    本文大致整理了自己用过的一些插件的使用感想(就是好不好用). 在那之前先简单贴两条插件须知,知道的可以忽略. 1.Alcatraz 类似于管理第三方库的cocoapods,管理插件也有个Alcatra ...

  4. 图片延迟加载jquery插件imgLazyLoading

    实现了图片延迟加载功能,插件代码非常简洁,且每个功能都把注释写得非常详细,适合网友们学习,好好利用哦 引入图片延迟加载Jquery插件文件后,页面使用代码如下: <script type=&qu ...

  5. 12个强大的Chrome插件

    Chrome功能强大,也得益于其拥有丰富的扩展资源库.Chrome Web Store里有各种各样的插件,可以满足你使用Chrome时的各种要求.和Firefox一样,Chrome的扩展非常容易安装, ...

  6. 【转】Xcode 插件优缺点对比(推荐 20 款插件)

    [转自]http://www.cnblogs.com/dsxniubility/p/5099191.html 1.Alcatraz 类似于管理第三方库的cocoapods,管理插件也有个Alcatra ...

  7. xcode 必用插件二

    本文大致整理了自己用过的一些插件的使用感想(就是好不好用). 在那之前先简单贴两条插件须知,知道的可以忽略. 1.Alcatraz 类似于管理第三方库的cocoapods,管理插件也有个Alcatra ...

  8. 精心挑选的12款优秀 jQuery Ajax 分页插件和教程

    在这篇文章中,我为大家收集了12个基于 jQuery 框架的 Ajax 分页插件,这些插件都提供了详细的使用教程和演示.Ajax 技术的出现使得 Web 项目的用户体验有了极大的提高,如今借助优秀的  ...

  9. 转:精心挑选的12款优秀 jQuery Ajax 分页插件和教程

    在这篇文章中,我为大家收集了12个基于 jQuery 框架的 Ajax 分页插件,这些插件都提供了详细的使用教程和演示.Ajax 技术的出现使得 Web 项目的用户体验有了极大的提高,如今借助优秀的  ...

随机推荐

  1. Solution -「Gym 102759G」LCS 8

    \(\mathcal{Description}\)   Link.   给定 \(m\),和长度为 \(n\),字符集为大写字母的字符串 \(s\),求字符集相同且等长的字符串 \(t\) 的数量,使 ...

  2. Solution -「LGR-087」「洛谷 P6860」象棋与马

    \(\mathcal{Description}\)   Link.   在一个 \(\mathbb R^2\) 的 \((0,0)\) 处有一颗棋子,对于参数 \(a,b\),若它当前坐标为 \((x ...

  3. Java并发基础之Compare And Swap/Set(CAS)

    什么是 CAS?CAS(Compare And Swap/Set)比较并交换, CAS 算法的过程是这样:它包含 3 个参数CAS(V,E,N). V 表示待更新的变量(内存值), E 表示预期值(旧 ...

  4. kafka3.x原理详解看这篇就够了

    一.概述 (一).kafka的定义 1.定义 1)kafka传统的定义:kafka是一个分布式的基于发布/订阅模式的消息队列,主要用于大数据实时处理领域 2)kafka最新的定义:kafka是一个开源 ...

  5. .NET 云原生架构师训练营(权限系统 系统演示 EntityAccess)--学习笔记

    目录 模块拆分 EntityAccess 模块拆分 EntityAccess 实体权限 属性权限 实体权限 创建 student https://localhost:7018/Student/dotn ...

  6. ctf平台

    CTF靶场 蓝鲸安全:http://whalectf.xin bugku:https://ctf.bugku.com XCTF攻防世界:https://adworld.xctf.org.cn/ i春秋 ...

  7. SaccadeNet:使用角点特征进行two-stage预测框精调 | CVPR 2020

    SaccadeNet基于中心点特征进行初步的目标定位,然后利用初步预测框的角点特征以及中心点特征进行预测框的精调,整体思想类似于two-stage目标检测算法,将第二阶段的预测框精调用的区域特征转化为 ...

  8. BI开创者Tableau“出走中国”,中国BI用户该何去何从?

    11月,Tableau在发给客户的邮件中透露将停止中国的直销业务,加入阿里的合作体系.消息来的如此突然,Tableau的同仁.合作伙伴.客户.用户.爱好者,甚至友商,无一不感到震惊和担忧. 在我们数据 ...

  9. 数据分析六个步骤,一款BI工具即可全部搞定

    数据分析是将大量的数据转化为有价值的信息,以求最大化地利用数据的功能,发挥数据的作用.数据分析的类型可以分为现状分析.原因分析.预测分析,按流程分为以下6个步骤: (1) 明确数据分析目的和思路 明确 ...

  10. 为什么说国产BI更适合国内企业?

    ​就算国外BI发展迅速,产品更加完善成熟,但对国内的企业来说,使用起来难免"水土不服",何况还有服务对接过程中的繁琐程.今天就来讨论一下,国内BI和国外BI到底该怎么选择? 国外B ...