英文:
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: "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
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<AsyncResult<OperationResponse>> 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("savePetPicture", routingContext -> {
Set<FileUpload> fileUploadSet = routingContext.fileUploads();
Iterator<FileUpload> 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 邮件列表中有一系列讨论帖子,我鼓励您阅读以了解这个问题:
- https://groups.google.com/g/vertx/c/vw-wnqK_ckE/m/amqEBm0aAQAJ
- https://groups.google.com/g/vertx/c/AnZ0ZiSgiqQ/m/lb7f7r1XDAAJ
- https://groups.google.com/g/vertx/c/42mJE3FqqUA
如果您需要实现一个处理文件上传的 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:
- https://groups.google.com/g/vertx/c/vw-wnqK_ckE/m/amqEBm0aAQAJ
- https://groups.google.com/g/vertx/c/AnZ0ZiSgiqQ/m/lb7f7r1XDAAJ
- https://groups.google.com/g/vertx/c/42mJE3FqqUA
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论