openapi开放平台架构_适用于所有人的MicroProfile OpenAPI
openapi开放平台架构
MicroProfile OpenAPI主要用于将OpenAPI添加到JAX-RS端点。 在这篇博客中,我们将研究SmallRye实现如何在Quarkus中使用时,通过一些额外的功能来扩展此功能,并支持更多的Web框架。
使用Quarkus
此处提供示例代码。 您也可以使用code.quarkus.io初始化项目-只需确保包括SmallRye OpenAPI扩展即可。
JAX-RS
让我们从Quarkus中的基本JAX-RS示例开始。 我们有一个Greeting
对象,其中包含一条message
和一个to
字段,我们将为该问候创建GET
, POST
和DELETE
端点。
除了通常的Quarkus设置外,您还需要pom.xml
的以下内容来创建JAX-RS端点:
<dependency><groupId>io.quarkus</groupId><artifactId>quarkus-smallrye-openapi</artifactId></dependency><dependency><groupId>io.quarkus</groupId><artifactId>quarkus-resteasy</artifactId></dependency><dependency><groupId>io.quarkus</groupId><artifactId>quarkus-resteasy-jsonb</artifactId></dependency>
在Quarkus中,您不需要Application
类,我们只需添加Endpoint类:
@Path ( "/jax-rs" )@Produces (MediaType.APPLICATION_JSON)@Consumes (MediaType.APPLICATION_JSON)public class JaxRsGreeting { @GET@Path ( "/hello" )public Greeting helloJaxRs() {return new Greeting( "Hello" , "JAX-RS" );}@POST@Path ( "/hello" )public Greeting newHelloJaxRs(Greeting greeting) {return greeting;}@DELETE@Path ( "/hello/{message}" )public void deleteHelloJaxRs( @PathParam ( "message" ) String message) {// Here do the delete.}}
到目前为止,我们还没有添加任何MicroProfile OpenAPI注释,但是由于我们添加了quarkus-smallrye-openapi
扩展,因此我们已经在/openapi
一个Schema文档:
---openapi: 3.0 . 3info:title: Generated APIversion: "1.0"paths:/jax-rs/hello:get:responses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'post:requestBody:content:application/json:schema:$ref: '#/components/schemas/Greeting'responses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'/jax-rs/hello/{message}:delete:parameters:- name: messagein: pathrequired: trueschema:type: stringresponses:"204" :description: No Contentcomponents:schemas:Greeting:type: objectproperties:message:type: stringto:type: string
有关更多信息,请参见quarkus.io/guides/rest-json 。
OpenAPI的
您可以使用MicroProfile OpenAPI将更多信息添加到生成的架构文档中。
使用配置头信息
我们添加到SmallRye的一项功能是能够添加标头信息的功能,您通常可以使用MicroProfile配置使用批注将标头信息添加到Application
类。 这在不需要Application
类的Quarkus中很有用。 因此,将以下内容添加到application.properties
将为您提供一些标题信息:
mp.openapi.extensions.smallrye.info.title=OpenAPI for Everyone%dev.mp.openapi.extensions.smallrye.info.title=OpenAPI for Everyone (development) %dev.mp.openapi.extensions.smallrye.info.title=OpenAPI Everyone (development)%test.mp.openapi.extensions.smallrye.info.title=OpenAPI for Everyone (test)mp.openapi.extensions.smallrye.info.version= 1.0 . 0mp.openapi.extensions.smallrye.info.description=Example on how to use OpenAPI everywheremp.openapi.extensions.smallrye.info.contact.email=phillip.kruger @redhat .commp.openapi.extensions.smallrye.info.contact.name=Phillip Krugermp.openapi.extensions.smallrye.info.contact.url=https: //www.phillip-kruger.commp.openapi.extensions.smallrye.info.license.name=Apache 2.0mp.openapi.extensions.smallrye.info.license.url=http: //www.apache.org/licenses/LICENSE-2.0.html
现在,在/openapi
下/openapi
生成的模式文档的/openapi
:
---openapi: 3.0 . 3info:title: OpenAPI for Everyone (development) title: OpenAPI Everyone (development)description: Example on how to use OpenAPI everywherecontact:name: Phillip Krugerurl: https: //www.phillip-kruger.comemail: phillip.kruger @redhat .comlicense:name: Apache 2.0url: http: //www.apache.org/licenses/LICENSE-2.0.htmlversion: 1.0 . 0 # rest of the schema document ...
向您的操作添加一些OpenAPI注释
您可以使用MicroProfile OpenAPI中的任何注释来进一步描述您的端点,例如Tag
注释:
@Path ( "/jax-rs" )@Produces (MediaType.APPLICATION_JSON)@Consumes (MediaType.APPLICATION_JSON)@Tag (name = "JAX-RS Resource" , description = "Basic Hello World using JAX-RS" )public class JaxRsGreeting {\\...}
自动生成操作ID
使用架构文档生成客户端存根的某些工具需要在架构文档中用于命名客户端存根方法的operationId
。 我们在SmallRye中添加了支持,以使用方法名称( METHOD
),类和方法名称( CLASS_METHOD
)或包,类和方法名称( PACKAGE_CLASS_METHOD
)自动生成此支持。 为此,将以下内容添加到application.properties
:
mp.openapi.extensions.smallrye.operationIdStrategy=METHOD
现在,您将看到operationId
每个操作模式文件中:
---openapi: 3.0 . 3 # header omitted .../jax-rs/hello:get:tags:- JAX-RS ResourceoperationId: helloJaxRsresponses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'post:tags:- JAX-RS ResourceoperationId: newHelloJaxRsrequestBody:content:application/json:schema:$ref: '#/components/schemas/Greeting'responses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'/jax-rs/hello/{message}:delete:tags:- JAX-RS ResourceoperationId: deleteHelloJaxRsparameters:- name: messagein: pathrequired: trueschema:type: stringresponses:"204" :description: No Content
更改OpenAPI版本
某些API网关可能需要某些OpenAPI版本才能工作。 由SmallRye扩展生成模式文档将与生成3.0.3
的版本,但因为有这些版本之间只有细微的差别,您可以更改到3.0.0
, 3.0.1
或3.0.2
。 您可以通过在application.properties
添加它来实现:
mp.openapi.extensions.smallrye.openapi= 3.0 . 2
现在生成的版本将是:
---openapi: 3.0 . 2 # Rest of the document ...
有关更多信息,请参见quarkus.io/guides/openapi-swaggerui 。
Spring网
最近在SmallRye中添加了对Spring Web的支持,这意味着,当您在Quarkus中使用Spring Web时,不仅会看到默认的OpenAPI文档,而且还可以使用MicroProfile OpenAPI进一步描述您的Spring Web端点。
让我们在当前应用程序中添加一个Spring Rest Controller。 首先将其添加到pom.xml
:
<dependency><groupId>io.quarkus</groupId><artifactId>quarkus-spring-web</artifactId></dependency>
现在,您可以使用Spring Web创建一个与到目前为止我们所研究的JAX-RS类似的端点:
@RestController@RequestMapping (value = "/spring" , produces = MediaType.APPLICATION_JSON_VALUE)@Tag (name = "Spring Resource" , description = "Basic Hello World using Spring" )public class SpringGreeting { @GetMapping ( "/hello" )public Greeting helloSpring() {return new Greeting( "Hello" , "Spring" );}@PostMapping ( "/hello" )public Greeting newHelloSpring( @RequestBody Greeting greeting) {return greeting;}@DeleteMapping ( "/hello/{message}" )public void deleteHelloSpring( @PathVariable (name = "message" ) String message) { }}
Spring注释将被扫描,并将其添加到您的模式文档中:
---openapi: 3.0 . 3 # header omitted .../spring/hello:get:tags:- Spring ResourceoperationId: helloSpringresponses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'post:tags:- Spring ResourceoperationId: newHelloSpringrequestBody:content:'*/*' :schema:$ref: '#/components/schemas/Greeting'responses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'/spring/hello/{message}:delete:tags:- Spring ResourceoperationId: deleteHelloSpringparameters:- name: messagein: pathrequired: trueschema:type: stringresponses:"204" :description: No Content
有关更多信息,请参见quarkus.io/guides/spring-web 。
Vert.xReact路线
在Quarkus中,您还可以使用React式路由来构建Vert.x端点。 与Spring Web相似,您的端点将在OpenAPI架构中可用,并且可以使用MicroProfile OpenAPI进行进一步描述。 要在Quarkus中添加Vert.xReact性路由,您需要在pom.xml
添加以下内容:
<dependency><groupId>io.quarkus</groupId><artifactId>quarkus-vertx-web</artifactId></dependency>
现在您可以创建端点:
@ApplicationScoped@RouteBase (path = "/vertx" , produces = "application/json" )@Tag (name = "Vert.x Resource" , description = "Basic Hello World using Vert.x" )public class VertxGreeting { @Route (path = "/hello" , methods = HttpMethod.GET)public Greeting helloVertX() {return new Greeting( "Hello" , "Vert.x" );}@Route (path = "/hello" , methods = HttpMethod.POST)public Greeting newHelloVertX( @Body Greeting greeting) {return greeting;}@Route (path = "/hello/:message" , methods = HttpMethod.DELETE)public void deleteHelloVertX( @Param ( "message" ) String message) { }}
现在您的Vert.x路由在OpenAPI中可用:
---openapi: 3.0 . 3 # header omitted .../vertx/hello:get:tags:- Vert.x ResourceoperationId: helloVertXresponses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'post:tags:- Vert.x ResourceoperationId: newHelloVertXrequestBody:content:'*/*' :schema:$ref: '#/components/schemas/Greeting'responses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'/vertx/hello/{message}:delete:tags:- Vert.x ResourceoperationId: deleteHelloVertXparameters:- name: messagein: pathrequired: trueschema:type: stringresponses:"204" :description: No Content
有关更多信息,请参见quarkus.io/guides/reactive-routes 。
用Panache生成的端点
在Quarkus中,您可以使用Panache生成您的JAX-RS端点。 如果您在pom.xml
具有quarkus-smallrye-openapi
扩展名,这些生成的类也将被扫描并添加到OpenAPI模式文档中。
有关更多信息,请参见quarkus.io/guides/rest-data-panache 。
任何其他Web框架
您还可以通过在yaml
文件中提供Schema文档的该部分来向文档添加任何其他端点。 举例来说,假设您有一个Servlet,它公开了一些方法,而您想将这些方法添加到架构文档中。 Servlet只是一个例子,任何Web框架都可以在这里工作。
因此,首先我们将其添加到pom.xml
以在Quarkus中添加Servlet支持:
<dependency><groupId>io.quarkus</groupId><artifactId>quarkus-undertow</artifactId></dependency>
现在,我们可以例如创建一个Servlet端点:
@WebServlet ( "/other/hello/*" )public class ServletGreeting extends HttpServlet { private static final Jsonb JSONB = JsonbBuilder.create(); @Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType( "application/json" );Greeting greeting = new Greeting( "Hello" , "Other" );PrintWriter out = response.getWriter();out.print(JSONB.toJson(greeting));}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType( "application/json" );Greeting greeting = JSONB.fromJson(request.getInputStream(), Greeting. class );PrintWriter out = response.getWriter();out.print(JSONB.toJson(greeting));}@Overrideprotected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// Here do the delete}}
现在,我们需要一个映射到这些端点的OpenAPI Schema文档。 您需要将此添加到src/main/resources/META-INF
名为openapi.yml
的文件中:
---openapi: 3.0 . 3tags:- name: Other Resourcedescription: Basic Hello World using Something elsepaths:/other/hello:get:tags:- Other ResourceoperationId: helloOtherresponses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'post:tags:- Other ResourceoperationId: newHelloOtherrequestBody:content:application/json:schema:$ref: '#/components/schemas/Greeting'responses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'/other/hello/{message}:delete:tags:- Other ResourceoperationId: deleteHelloOtherparameters:- name: messagein: pathrequired: trueschema:type: stringresponses:"204" :description: No Content
它将与其他端点合并,以显示文档中的所有路径。 因此,最后您的/openapi
输出将如下所示:
---openapi: 3.0 . 2info:title: OpenAPI for Everyone (development) title: OpenAPI Everyone (development)description: Example on how to use OpenAPI everywherecontact:name: Phillip Krugerurl: https: //www.phillip-kruger.comemail: phillip.kruger @redhat .comlicense:name: Apache 2.0url: http: //www.apache.org/licenses/LICENSE-2.0.htmlversion: 1.0 . 0tags:- name: Other Resourcedescription: Basic Hello World using Something else- name: Spring Resourcedescription: Basic Hello World using Spring- name: JAX-RS Resourcedescription: Basic Hello World using JAX-RS- name: Vert.x Resourcedescription: Basic Hello World using Vert.xpaths:/other/hello:get:tags:- Other ResourceoperationId: helloOtherresponses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'post:tags:- Other ResourceoperationId: newHelloOtherrequestBody:content:application/json:schema:$ref: '#/components/schemas/Greeting'responses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'/other/hello/{message}:delete:tags:- Other ResourceoperationId: deleteHelloOtherparameters:- name: messagein: pathrequired: trueschema:type: stringresponses:"204" :description: No Content/jax-rs/hello:get:tags:- JAX-RS ResourceoperationId: helloJaxRsresponses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'post:tags:- JAX-RS ResourceoperationId: newHelloJaxRsrequestBody:content:application/json:schema:$ref: '#/components/schemas/Greeting'responses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'/jax-rs/hello/{message}:delete:tags:- JAX-RS ResourceoperationId: deleteHelloJaxRsparameters:- name: messagein: pathrequired: trueschema:type: stringresponses:"204" :description: No Content/spring/hello:get:tags:- Spring ResourceoperationId: helloSpringresponses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'post:tags:- Spring ResourceoperationId: newHelloSpringrequestBody:content:'*/*' :schema:$ref: '#/components/schemas/Greeting'responses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'/spring/hello/{message}:delete:tags:- Spring ResourceoperationId: deleteHelloSpringparameters:- name: messagein: pathrequired: trueschema:type: stringresponses:"204" :description: No Content/vertx/hello:get:tags:- Vert.x ResourceoperationId: helloVertXresponses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'post:tags:- Vert.x ResourceoperationId: newHelloVertXrequestBody:content:'*/*' :schema:$ref: '#/components/schemas/Greeting'responses:"200" :description: OKcontent:application/json:schema:$ref: '#/components/schemas/Greeting'/vertx/hello/{message}:delete:tags:- Vert.x ResourceoperationId: deleteHelloVertXparameters:- name: messagein: pathrequired: trueschema:type: stringresponses:"204" :description: No Contentcomponents:schemas:Greeting:type: objectproperties:message:type: stringto:type: string
它包含来自JAX-RS,Spring Web,Vert.xReact性路由和Servlet的资源。
招摇UI
在Quarkus中,默认情况下包括Swagger-UI,当您现在浏览到localhost:8080 / swagger-ui时,您将看到带有所有端点的UI:
概要
在本文中,我们研究了Quarkus如何扩展MicroProfile OpenAPI规范,以使其更易于记录端点。 我们还研究了如何使用它来记录任何Web框架。
如果您发现任何问题或有任何建议,请转到SmallRye项目,然后在此处进行讨论。
翻译自: https://www.javacodegeeks.com/2020/08/microprofile-openapi-for-everyone.html
openapi开放平台架构
openapi开放平台架构_适用于所有人的MicroProfile OpenAPI相关推荐
- java接口开放平台设计,OpenApi开放平台架构实践
WebAPI 开放平台架构实践 导读 背景 需求 场景 架构设计 总结 背景 随着业务的发展,越来越多不同系统之间需要数据往来,我们和外部系统之间产生了数据接口的对接.当然,有我们提供给外部系统(工具 ...
- 开放平台架构_三步画出产品业务架构图
今天有位同学询问,如何才能画出"高大上"的业务架构图. 他说:"最近公司要绘制XX行业的解决方案,需要产品经理画出整体架构图,但是自己没接触过,不知道如何下手." ...
- php openapi设计,OpenApi开放平台架构实践
随着业务的发展,越来越多不同系统之间需要数据往来,我们和外部系统之间产生了数据接口的对接.当然,有我们提供给外部系统(工具)的,也有我们调用第三方的.而这里重点讲一下我们对外的接口. 目前,我们运营和 ...
- swagger openapi开放平台 pyhton3.7实现http发送请求,pyhon中代码中发送http请求控制4g物联网开关
swagger openapi开放平台 pyhton3.7实现http发送请求 pyhon中代码中控制 4g物联网开关,此代码与python2.7不兼容,具体体现在加密解: get_authoriza ...
- SDCC讲师预热专访:淘宝岑文初谈开放平台架构
CSDN年度技术盛宴 2012 SDCC中国软件开发者大会将于 2012年9月8日-9日在 北京 • 国家会议中心举行. 软件研发频道 将采访一些与会讲师,谈谈他们将在会上分享的内容. 本期我们采访的 ...
- 淘宝开放平台架构整理资料
一.淘宝开放平台在架构方面的特点.在系统稳定和数据安全性方面用到的技术 淘宝开放平台架构关键词:透明,核心模块小,按需简化设计,多层次设计配合(js,client,server),服务模式创新. 系统 ...
- 接口大师,即刻构建你的OpenAPI+开放平台
接口大师能做什么? 接口大师,原名:PhalApi专业版.是一套开发.管理和提供接口的软件源代码和解决方案. 当你: 1.需要自己使用API接口时,接口大师可以: 专注接口低代码开发,为您提供API代 ...
- 百度云和ai开放平台关系_集成平台即服务,云和……独角兽
百度云和ai开放平台关系 啊,是的,这个虚幻的乌托邦"云"中,独角兽嬉戏,蝴蝶纷飞,魔术精灵为我拿来了四分之三的啤酒. 我爱那个地方. 我刚刚在羊皮服装公司的一家开源狼网站上看到了 ...
- 快速 开发平台 架构_快速介绍清洁架构
快速 开发平台 架构 by Daniel Deutsch 由Daniel Deutsch 快速介绍清洁架构 (A quick introduction to clean architecture) I ...
最新文章
- linux怎么创建用户教程,在Linux中如何手动创建一个用户
- 为什么不要用uuid做主键
- 【F#2.0系列】介绍String类型
- 2019_8_1python
- 存储设备分区,格式化,挂载
- 电工结业试卷_电工技术基础结业考试试卷
- PHP判断字符串是纯英文、纯汉字或汉英混合
- python list的+=操作
- 素数就是不能再进行等分的数。比如2,3,5,7,11,等 9=3*3说明它可以等分,因而不是素数 我们国家在1949年建国,如果只给你 1 9 4 9 这4个数字卡片, 可以随意摆放他们的先后顺序(但
- Node.js 学习笔记 - 学习《深入浅出Node.js》-朴灵
- 市场监管总局通报部分转供电主体不执行国家电价政策案例
- 汇编 LED驱动 烧写bin文件到SD卡
- 【Unity】添加 Device Simulator功能
- Attiny48单片机编程经验总结
- luogu1969积木游戏
- ROS2探索(一)Publisher-Subscriber的内部过程
- windows控制台cmd查看wifi密码
- CSS属性多个子容器时使用flex-shrink 计算比例
- GStreamer播放教程05——色彩平衡
- 汇编与接口技术期末复习笔记(1)—— 汇编部分