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

问题描述

我正在尝试使用Vert.x Web API合同通过OpenAPI操作定义定义服务接口,在Rest API中上传文件以及其他相关数据:-

/api/pets/{petId}/uploadImage:
post:
  summary: upload file for pet
  operationId: savePetPicture
  x-vertx-event-bus: "petstore_manager.myapp"
  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

我有一个服务接口PetStoreManagerService,其中定义了一些方法来处理该资源的操作。

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

和Model类定义如下

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

我在项目中遵循了本教程。 vertx-web-api-service tutorial 我的问题是此PetImage类中的profileImage类型应该是什么?在我的服务实现类中,requestBody的值为空。

我发现一些帖子在MainVerticle类中将操作处理为

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

我想知道是否可以在服务类中处理此问题,将请求路由到本教程中显示的PetStoreManagerService实现类。

解决方法

事件总线的目的不是为了处理较大的有效负载(主要是因为它缺少字节流),因此您不应该尝试通过它执行文件上传。

vert.x邮件列表上有一堆线程,我鼓励您阅读以解释该问题:

如果您需要实施处理文件上传的OpenAPI操作,则应使用按照here所述安装的普通Vert.x Web处理程序来实现它,并按照here进行检索文件上传>