[英]Spring WebClient does not decode application/octet-stream into File object
Hi I am using OpenAPI Generator Maven Plugin to generate some Java Client code (using Spring WebClient library).嗨,我正在使用 OpenAPI Generator Maven 插件生成一些 Java 客户端代码(使用 Spring WebClient 库)。 One of the endpoints of my spec.
我的规范的端点之一。 returns binary content, like:
返回二进制内容,例如:
"schema": {
"type": "string",
"format": "binary"
}
The generated code uses java.io.File
as the return type for that, like:生成的代码使用
java.io.File
作为返回类型,例如:
public Mono<ResponseEntity<File>> downloadWithHttpInfo(String filename) throws WebClientResponseException {
ParameterizedTypeReference<File> localVarReturnType = new ParameterizedTypeReference<File>() {};
return downloadRequestCreation(filename).toEntity(localVarReturnType);
}
When calling this generated method, the response code was 200 (ie OK from the server side), but I got the following error in my client code:调用此生成的方法时,响应代码为 200(即服务器端的 OK),但我的客户端代码中出现以下错误:
org.springframework.web.reactive.function.UnsupportedMediaTypeException:
Content type 'application/octet-stream' not supported for bodyType=java.io.File
This came from the toEntity()
method, which is part of the Spring WebClient code instead of my code.这来自
toEntity()
方法,它是 Spring WebClient 代码而不是我的代码的一部分。
Is there a way to workaround this?有没有办法解决这个问题? A: Instruct OpenAPI Generator Maven Plugin not to use
java.io.File
type but use Resource
type? A: 指示 OpenAPI Generator Maven 插件不要使用
java.io.File
类型但使用Resource
类型? B: Somehow make WebClient able to decode application/octet-stream into java.io.File
? B:不知何故,让 WebClient 能够将应用程序/八位字节流解码为
java.io.File
?
Found a solution: add the following options to the OpenAPI Generator Maven Plugin then generate the code again, which would replace File
to Resource
找到解决方案:将以下选项添加到 OpenAPI Generator Maven 插件,然后再次生成代码,这将替换
File
to Resource
<generatorName>java</generatorName>
<library>webclient</library>
<typeMappings>string+binary=Resource</typeMappings>
<importMappings>Resource=org.springframework.core.io.Resource</importMappings>
The above is saying when the return type is string
and the format is binary
, map it to Resource
and for Resource
import it as org.springframework.core.io.Resource
.上面是说当返回类型为
string
且格式为binary
时,将其映射到Resource
并将Resource
导入为org.springframework.core.io.Resource
。 There you go.你去吧。
I had the exact same issue, but using Gradle instead of Maven.我有完全相同的问题,但使用 Gradle 而不是 Maven。 Here is the syntax for doing the same in Gradle:
这是在 Gradle 中执行相同操作的语法:
task generateClientSources(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName = 'java'
// other configs ..
configOptions = [
// other configs ..
library : 'webclient'
]
typeMappings = [
File : 'Resource'
]
importMappings = [
File : 'org.springframework.core.io.Resource'
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.