OpenApi 生成的客户端在上传过程中失败,并出现 java.lang.OutOfMemoryError: Java 堆空间
我正在使用生成的 OpenAPI 客户端将文件 (~190mb) 上传到服务。 使用生成的方法时,代码会失败并出现 OutOfMemoryException。
如何告诉 OpenAPI 客户端对文件使用缓冲读取器/写入器?还有其他解决方案吗?
日志:
2022-02-22 22:37:45.569 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient : Content-Type: multipart/form-data; boundary=b75abfd1-9ae1-4c86-b69e-e444d341b051
2022-02-22 22:37:45.570 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient : Content-Length: 193464281
2022-02-22 22:37:45.570 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient : Authorization: Basic XXX
2022-02-22 22:37:45.570 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient : Accept: application/json
2022-02-22 22:37:45.571 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient : User-Agent: OpenAPI-Generator/0.0.13/java
2022-02-22 22:37:45.744 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient :
2022-02-22 22:37:49.588 ERROR 1 --- [ XNIO-1 task-3] o.z.problem.spring.common.AdviceTraits : Internal Server Error
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.OutOfMemoryError: Java heap space
代码:
private ApiClient login() {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setDebugging(true);
defaultClient.setBasePath(this.basepath);
// Configure HTTP basic authorization: Basic
HttpBasicAuth Basic = (HttpBasicAuth) defaultClient.getAuthentication("Basic");
Basic.setUsername(this.username);
Basic.setPassword(this.password);
return defaultClient;
}
private void upload() {
String filename = "file.file";
ApiClient client = login();
MediaApi mediaApi = new MediaApi();
mediaApi.setApiClient(client);
Media m = mediaApi.mediaCreate(new File(filename));
}
OpenApi:
"swagger": "2.0",
"info": {
"title": "MediaCMS API",
"version": "0.0.13"
},
"host": "localhost:8085",
"schemes": ["http"],
"basePath": "/api/v1",
"consumes": ["application/json"],
"produces": ["application/json"],
"securityDefinitions": {
"Basic": {
"type": "basic"
}
},
"security": [
{
"Basic": []
}
],
"paths": {
"/media": {
"post": {
"operationId": "media_create",
"summary": "Add new Media",
"description": "Adds a new media, for authenticated users",
"parameters": [
{
"name": "media_file",
"in": "formData",
"description": "media_file",
"required": true,
"type": "file"
},
{
"name": "description",
"in": "formData",
"description": "description",
"required": false,
"type": "string"
},
{
"name": "title",
"in": "formData",
"description": "title",
"required": false,
"type": "string"
}
],
"responses": {
"201": {
"description": "response description",
"schema": {
"$ref": "#/definitions/Media"
}
},
"401": {
"description": "bad request"
}
},
"consumes": ["multipart/form-data", "application/x-www-form-urlencoded"],
"tags": ["Media"]
}
}
}
}
I am using a generated OpenAPI Client for uploading a file (~190mb) to a service.
When using the generated method, the code fails with an OutOfMemoryException.
How can I tell the OpenAPI Client to use a buffered reader/writer for the file? Is there another solution?
Log:
2022-02-22 22:37:45.569 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient : Content-Type: multipart/form-data; boundary=b75abfd1-9ae1-4c86-b69e-e444d341b051
2022-02-22 22:37:45.570 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient : Content-Length: 193464281
2022-02-22 22:37:45.570 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient : Authorization: Basic XXX
2022-02-22 22:37:45.570 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient : Accept: application/json
2022-02-22 22:37:45.571 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient : User-Agent: OpenAPI-Generator/0.0.13/java
2022-02-22 22:37:45.744 INFO 1 --- [ XNIO-1 task-3] okhttp3.OkHttpClient :
2022-02-22 22:37:49.588 ERROR 1 --- [ XNIO-1 task-3] o.z.problem.spring.common.AdviceTraits : Internal Server Error
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.OutOfMemoryError: Java heap space
Code:
private ApiClient login() {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setDebugging(true);
defaultClient.setBasePath(this.basepath);
// Configure HTTP basic authorization: Basic
HttpBasicAuth Basic = (HttpBasicAuth) defaultClient.getAuthentication("Basic");
Basic.setUsername(this.username);
Basic.setPassword(this.password);
return defaultClient;
}
private void upload() {
String filename = "file.file";
ApiClient client = login();
MediaApi mediaApi = new MediaApi();
mediaApi.setApiClient(client);
Media m = mediaApi.mediaCreate(new File(filename));
}
OpenApi:
"swagger": "2.0",
"info": {
"title": "MediaCMS API",
"version": "0.0.13"
},
"host": "localhost:8085",
"schemes": ["http"],
"basePath": "/api/v1",
"consumes": ["application/json"],
"produces": ["application/json"],
"securityDefinitions": {
"Basic": {
"type": "basic"
}
},
"security": [
{
"Basic": []
}
],
"paths": {
"/media": {
"post": {
"operationId": "media_create",
"summary": "Add new Media",
"description": "Adds a new media, for authenticated users",
"parameters": [
{
"name": "media_file",
"in": "formData",
"description": "media_file",
"required": true,
"type": "file"
},
{
"name": "description",
"in": "formData",
"description": "description",
"required": false,
"type": "string"
},
{
"name": "title",
"in": "formData",
"description": "title",
"required": false,
"type": "string"
}
],
"responses": {
"201": {
"description": "response description",
"schema": {
"$ref": "#/definitions/Media"
}
},
"401": {
"description": "bad request"
}
},
"consumes": ["multipart/form-data", "application/x-www-form-urlencoded"],
"tags": ["Media"]
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论