系统接口集成调用

核心提示前面已经集成 MyBatis Plus、Druid 数据源,开发了 5 个接口。在测试这 5 个接口时使用了 HTTP Client 或 PostMan,无论是啥都比较麻烦:得自己写请求地址 URL、请求参数等,于是多年前就出现了 Swag

整合了MyBatis Plus和Druid数据源,开发了五个接口。测试这五个接口的时候,用的是HTTP客户端或者PostMan,不管是什么,都比较麻烦:要自己写请求地址URL,请求参数等。,所以霸气的东西很多年前就出现了。Swagger可以自动生成界面文档,方便地测试各个界面。不幸的是,MVN仓库上的Springfox Swagger2版本在2020年7月停止了,而这篇文章写于2022年8月。已经两年没有动静了。与此同时,springdoc-openapi悄然出现。

Doc Open API支持Open API 3、Swagger-ui等。它可以很容易地与Spring Boot集成,其配置和使用类似于Springfox Swagger2。

1个集成的Spring Doc

1.1添加依赖关系

Springdoc-openapi不是Spring framework官方团队开发的,而是一个社区项目,不包含在spring-boot-dependencies中。因此,您需要首先定义版本号:

....1.6.9

添加依赖项:

org . springdoc springdoc-openapi-ui $ { springdoc-openapi-ui . version }

在这个依赖项中使用Swagger-ui来显示HTML格式的文档。

1.2编写配置类

其实你可以写配置类,也可以不写。如果不写配置类,可以通过注释定义文档信息。创建类:com . yygnb . demo . config . springd config。

包com . yygnb . demo . config;导入io . swagger . v3 . OAS . models . external documentation;导入io . swagger . v3 . OAS . models . open API;导入io . swagger . v3 . OAS . models . info . info;导入org . spring framework . context . annotation . bean;导入org . spring framework . context . annotation . configuration;@ configuration public class SpringDocConfig { private String title = " Hero spring boot Demo ";private String description = "Hero演示Spring Boot的用法";私有字符串版本= " v 0 . 0 . 1 ";私有字符串websiteName = "英雄网站";私有字符串websiteUrl = " http://www . yygnb . com ";@ Bean public open API hero open API { return new open API . info . title。描述。version). external docs . description。网址);}}

上述配置定义了所显示文档的信息,与界面的对应关系如下:

在配置文件中,除了单据信息外,还可以配置单据分组、授权等。,这将在后面的企业级实战文章中详细介绍,所有微服务的接口都将集成在网关层spring cloud gateway中。

1.3配置yml

在application.yml中配置springdoc:

#接口文档spring doc:packages-to-scan:com . yygnb . demo . controller swagger-ui:enabled:true

这两项没有配置,默认情况下packages-to-scan是启动类所在的路径;Springdoc.swagger-ui.enabled默认为真,配置后可以在不同环境下开启或关闭。

1.4添加注释

由doc-open API和springfox-swagger2提供的注释完全不同:

修改实体类计算机,增加springdoc-openapi的注释:

@ Schema @ Data @ NoArgsConstructor @ AllArgsConstructor @ builder public类计算机实现Serializable { private static final long serialVersionUID = 1L;@TableId私有长id;@ Schema private BigDecimal size@Schema私有字符串操作;@Schema私有字符串year}

修改控制器计算机控制器并添加注释:

@ Tag @ RequiredArgsConstructor @ rest controller @ requestmapping public class computer controller { private final IComputerService computer service;@Operation @GetMapping公共计算机find byid @ path variable Long id){ return this . Computer service . get byid;} @Operation @Parameters,@Parameter }) @GetMapping公共页面find Page { return this . computer service . Page);} @Operation @PostMapping公共计算机保存{ computer.setIdthis . computer service . save;归还电脑;} @Operation @PutMapping公共计算机更新@PathVariable Long id,@ request body Computer Computer){ Computer . setid;this . computer service . update byid;归还电脑;} @ Operation @ delete mapping @ Parameter public void delete { this . computer service . remove byid;}}

1.5运行测试

启动服务并在浏览器中访问它:

http://localhost:9099/swagger-ui/index . html

2个api文档

文档标题下有一个小链接:/v3/api-docs。点击这个链接,大量的JSON数据会在新的页面中显示出来。

看似枯燥的数据,却意义重大。swagger-ui正是通过这些数据来呈现页面的。此外,这些JSON数据还可以在一定程度上简化前端开发以及前端和后端网络请求的工作量。

英雄-管理-用户界面

Hero-admin-ui是一个基于Vue 3+Typescript的开源项目,由Graceful开发,以基于JSON Schema的表单和列表为特色。JSON Schema可以快速呈现一个列表、一个表单,甚至是一个搜索页面和一个详细的表单页面。如果独立使用hero-admin-ui,JSON Schema需要手工编写。但是如果后端接口集成了Swagger或者Spring Doc,上面api-docs返回的JSON就包含了JSON Schema。两者结合可以快速实现搜索页面、表单页面等。目前,hero-admin-ui已经在npmjs上发布,并提交到github。可以搜索关键词hero-admin-ui查看一下。在后面的实用文章中,前端部分会用到这个组件库来实现前端页面。

3定制配置

在“1.2编写配置类”一节中,文档信息被死写在代码中。如果多个微服务需要集成spring doc,可以将前面写的SpringDocConfig提取到公共模块中,通过maven依赖引用在application.yml中配置不同的变量。

3.1添加依赖关系

spring framework . boot spring-boot-configuration-处理器true

此依赖项配置的目的是在编写application.yml时,用户定义的属性具有代码提示。它会生成配置元数据,不需要你自己手动编写。

3.2定义配置的实体类

创建com.yygnb.demo.config.DocInfo,将SpringDocConfig中所有死属性都移到这个配置实体类中:

@ Data @ Component @ configuration properties public class DocInfo { private String Title = " Demo Title ";私有字符串描述= "演示描述";私有字符串版本= " v 0 . 0 . 1 ";私有字符串websiteName = "演示网站";私有字符串websiteUrl = " http://www . yygnb . com ";}

comment @ configuration properties声明配置属性,配置application.yml时可以使用doc-info。

3.3重建SpringDocConfig

将DocInfo引入SpringDocConfig,通过构造函数注入:

@ RequiredArgsConstructor @ configuration public类SpringDocConfig { private final DocInfo DocInfo;@ Bean public open API springShopOpenAPI { return new open API . info . title)。描述)。version)). external docs . description)。网址));}}

补充一个小点:lombok中提供了注释@ requireArgsconstructor,相当于在类中编写方法:

public SpringDocConfig { this . docInfo = docInfo;}

构造注入时,在构造函数中写很多属性是没有意义的。既然已经使用了@Data注释,为什么不使用@RequiredArgsConstructor呢?

3.4使用自定义配置

定制已经完成,DocInfo对应的配置可以用在application.yml:

文档信息:标题:SpringBoot演示文稿描述:学习Spring Boot 2.7.2

DocInfo的所有属性都定义了默认值,这些值可以在application.yml中被覆盖,比如上面的title和description属性。重新启动服务以查看运行效果:

4把集成刀4j

在之前的springfox-swagger时代,很多同学不喜欢swagger-ui的界面风格,会集成knife4j的ui。Spring Doc也可以集成knife4j。

如果要使用knife4j,需要在Spring Doc的配置中添加分组配置。这里我们添加最简单的分组配置。

com . yygnb . demo . config . springdocconfig

@ RequiredArgsConstructor @ configuration public类SpringDocConfig { private final DocInfo DocInfo;@ Bean public open API hero open API { return new open API . info . title)。描述)。version)). external docs . description)。网址));} @ Bean public GroupedOpenApi public API { return GroupedOpenApi . builder。组)。pathsToMatch。建造;}}

如果不添加这个GroupedOpenApi实例,将不会显示knife4j ui。

将knife4j引入pom.xml

...3.0.3 ...com . github . xiaoyi min knife 4j-spring doc-ui $ { knife 4j-spring doc-ui . version }

启动服务,访问:

http://localhost:9099/doc.html

显示knife4j的用户界面:

资料来源:https://www.cnblogs.com/youyacoder/p/16562539.html

 
友情链接
鄂ICP备19019357号-22