Vertx Web API 服务 使用 OpenAPI3RouterFactory 和 EventBus 进行文件上传

huangapple go评论56阅读模式
英文:

Vertx Web API Service File uploading using OpenAPI3RouterFactory and EventBus

问题

我正在尝试使用 Vert.x Web API 中的 Rest API 上传文件和其他相关数据通过定义带有 OpenAPI 操作定义的服务接口的方式如下

    /api/pets/{petId}/uploadImage:
    post:
      summary: 上传宠物文件
      operationId: savePetPicture
      x-vertx-event-bus: "petstore_manager.myapp"
      parameters:
        - name: petId
          in: path
          required: true
          description: 宠物的 ID
          schema:
            type: string
      requestBody:
        description: 用作宠物图片的图像
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                profileImage:
                  type: string
                  format: binary
                name:
                  type: string

我有一个名为 PetStoreManagerService 的服务接口我在这里定义了处理此资源操作的方法

    @WebApiServiceGen
    public interface PetStoreManagerService {
        void savePetPicture(PetImage body, String petId, OperationRequest operationRequest,
                          Handler<AsyncResult<OperationResponse>> handler);
    }

模型类定义如下

    @DataObject(generateConverter = true, publicConverter = false)
    public class PetImage {
      String name;
      Buffer profileImage;
    }

我在项目中遵循了这个教程[vertx-web-api-service 教程][1]
我的问题是 PetImage 类中的 profileImage 应该是什么类型在我的服务实现类中requestBody 为 null

我在一些帖子中发现了一些操作是在 MainVerticle 类中处理的如下所示

    routerFactory.addHandlerByOperationId("savePetPicture",  routingContext -> {
    Set<FileUpload> fileUploadSet = routingContext.fileUploads();
    Iterator<FileUpload> fileUploadIterator = fileUploadSet.iterator();
    // ...

我想知道是否可以在服务类中处理这个操作将请求路由到 PetStoreManagerService 实现类就像教程中所示

  [1]: https://vertx.io/docs/vertx-web-api-service/java/#_mount_to_router_factory
英文:

I am trying to upload file with other related data in Rest API using Vert.x Web API Contract defining service interface with OpenAPI operations definition as follows:-

/api/pets/{petId}/uploadImage:
post:
summary: upload file for pet
operationId: savePetPicture
x-vertx-event-bus: &quot;petstore_manager.myapp&quot;
parameters:
- name: petId
in: path
required: true
description: The id of the pet
schema:
type: string
requestBody:
description: image to be used as pet picture
content:
multipart/form-data:
schema:
type: object
properties:
profileImage:
type: string
format: binary
name:
type: string

I have a service interface PetStoreManagerService where I have defined methods to handle the operations for this resource.

@WebApiServiceGen
public interface PetStoreManagerService {
void savePetPicture(PetImage body, String petId, OperationRequest operationRequest,
Handler&lt;AsyncResult&lt;OperationResponse&gt;&gt; handler);
}

and Model class is defined as follows

@DataObject(generateConverter = true, publicConverter = false)
public class PetImage {
String name;
Buffer profileImage;
}

I have followed this tutorial in my project. vertx-web-api-service tutorial
My question is what should be the type of profileImage in this PetImage class? I am getting null for the requestBody in my service implementation class.

I have found some post where the operation is handled in MainVerticle class as

routerFactory.addHandlerByOperationId(&quot;savePetPicture&quot;,  routingContext -&gt; {
Set&lt;FileUpload&gt; fileUploadSet = routingContext.fileUploads();
Iterator&lt;FileUpload&gt; fileUploadIterator = fileUploadSet.iterator();
// ...

I was wondering if this could be handled in service class, routing the request to PetStoreManagerService implementation class as it is shown in the tutorial.

答案1

得分: 0

事件总线并非设计用于处理大数据负载(主要因为它缺乏字节流支持),因此您不应尝试通过它执行文件上传。

在 vert.x 邮件列表中有一系列讨论帖子,我鼓励您阅读以了解这个问题:

如果您需要实现一个处理文件上传的 OpenAPI 操作,您应该使用通常的 Vert.x Web 处理程序来实现,如 此处 所述,并如 此处 所述检索文件上传。

英文:

The Event Bus is not designed to handle large payloads (mostly because it lacks byte streaming), hence you should not try to perform file uploads through it.

There are a bunch of thread on vert.x mailing list I encourage you to read that explains the issue:

If you need to implement an OpenAPI operation that handles file uploads, you should implement it using the usual Vert.x Web Handler mounted as explained here and retrieve the file uploads as explained here

huangapple
  • 本文由 发表于 2020年9月2日 21:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63706692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定