SpringBoot引入Knife4j(增强版Swagger)为Java MVC框架生成api文档

本文主要是介绍SpringBoot引入Knife4j(增强版Swagger)为Java MVC框架生成api文档,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案,前身是swagger-bootstrap-ui,取名knife4j是希望她能像一把匕首一样小巧,轻量,并且功能强悍!

 

快速开始

添加maven依赖

<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>2.0.3</version>
</dependency>

配置文件配置

@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfiguration {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("2.1版本").select()// 这里指定Controller扫描包路径.apis(RequestHandlerSelectors.basePackage("com.jaemon.app.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("app项目接口文档").description("app项目").termsOfServiceUrl("http://IP:PORT/{contextPath}/doc.html").contact(new Contact("Jaemon", "http://answer", "answer_ljm@163.com")).license("app service").licenseUrl("https://www.github.com").version("1.0").build();}
}

访问: http://IP:PORT/{contextPath}/doc.html
eg. http://localhost:8888/oms/doc.html

 

入门使用

实体类

@Data
@ApiModel("用户查询请求对象")
public class UserReqDTO {@ApiModelProperty(notes = "用户姓名")private String userName;@ApiModelProperty(notes = "登录账号")private String loginName;
}
@Data
@ApiModel("用户返回视图对象")
@AllArgsConstructor
public class UserVO {@ApiModelProperty(required = true, notes = "用户id")private Long id;@ApiModelProperty(required = true, notes = "用户姓名")private String userName;@ApiModelProperty(required = true, notes = "登录账号")private String loginName;
}
@ApiModel("通用接口返回对象")
@Data
@AllArgsConstructor
public class Result<T> {@ApiModelProperty(required = true, notes = "响应码", example = "0")private int code;@ApiModelProperty(required = true, notes = "响应描述", example = "成功")private String msg;@ApiModelProperty(notes = "响应数据")private T data;
}

控制层

@Api(value = "", tags = "测试接口")
// 大分类顺序
@ApiSort(value = 1)
@RestController
@RequestMapping(value = "/demo")
public class DemoController {@PostMapping("/userList")@ApiOperation(value = "userList接口", notes = "查询用户列表")@ApiResponses(value = {@ApiResponse(code = 0, message = "请求成功"),@ApiResponse(code = 1, message = "请求失败")})public Result userList(@RequestBody UserReqDTO userReqDTO) {List<UserVO> userVOS = Lists.newArrayList();UserVO userVO;for (int i = 0; i < 5; i++) {userVO = new UserVO((long)i, "Jaemon" + i, "Jaemon" + i);userVOS.add(userVO);}return new Result(0, "成功", userVOS);}@ApiOperation(value = "findByUserId接口", notes = "根据用户id查询用户信息")@ApiImplicitParam(name = "id", value = "用户id", paramType = "path")@GetMapping("/findByUserId/{id}")public Result<UserVO> findByUserId(@PathVariable("id") Long id) {UserVO userVO = new UserVO(id, "Jaemon", "Jaemon");return new Result(0, "成功", userVO);}}

 

常用注解说明

  • @Api: 用在类上,说明该类的作用
  • @ApiOperation: 用在方法上,说明方法的作用,标注在具体请求上,value和notes的作用差不多,都是对请求进行说明;tags则是对请求进行分类的,比如你有好几个controller,分别属于不同的功能模块,那这里我们就可以使用tags来区分了,看上去很有条理
  • @ApiImplicitParams: 用在方法上包含一组参数说明
  • @ApiImplicitParam: 用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
    • paramType:参数放在哪个地方
    • header 请求参数的获取:@RequestHeader
    • query 请求参数的获取:@RequestParam
    • path(用于restful接口) 请求参数的获取:@PathVariable
    • body(不常用)
    • form(不常用)
    • name: 参数名
    • dataType: 参数类型
    • required: 参数是否必须传
    • value: 参数的意思
    • defaultValue: 参数的默认值
  • @ApiResponses: 用于表示一组响应
  • @ApiResponse: 用在@ApiResponses中,一般用于表达一个错误的响应信息
    • code: 数字,例如400
    • message: 信息,例如”请求参数没填好”
    • response: 抛出异常的类
  • @ApiModel: 描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)表明这是一个被swagger框架管理的model,用于class上
  • @ApiModel: 使用在实体类上,描述实体类。
  • @ApiModelProperty : 使用在实体类上的成员变量上,描述成员变量的含义。

 

SpringCloud微服务架构中使用

在Spring Cloud的微服务架构下,每个微服务其实并不需要引入前端的Ui资源,因此在每个微服务的Spring Boot项目下,引入knife4j提供的微服务starter

<dependency>    <groupId>com.github.xiaoymin</groupId>    <artifactId>knife4j-micro-spring-boot-starter</artifactId>    <version>${knife4j.version}</version>
</dependency>

在网关聚合文档服务下,可以再把前端的ui资源引入

<dependency>    <groupId>com.github.xiaoymin</groupId>    <artifactId>knife4j-spring-boot-starter</artifactId>    <version>${knife4j.version}</version>
</dependency>

 

Reference

  • knife4j
  • Swagger 增强(knife4j)自动生成Api 文档(SpringBoot & SpringCloud Gateway自动配置

这篇关于SpringBoot引入Knife4j(增强版Swagger)为Java MVC框架生成api文档的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1005402

相关文章

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为