简体   繁体   English

Spring WebClient 不将应用程序/八位字节流解码为文件 object

[英]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.

相关问题 无法在Spring / Postman中上传多部分文件和dto对象内容类型&#39;application / octet-stream&#39;不支持 - Unable to upload multipart file and dto object in Spring/Postman Content type 'application/octet-stream' not supported application/octet-stream 用于文件上传 - application/octet-stream for file Upload 具有应用程序/八位字节流的Spring Boot multipartFile引发异常 - spring boot multipartFile with application/octet-stream throws exception spring controller 如何处理应用程序/八位字节流请求? - How can a spring controller deal with application/octet-stream request? 为什么MimetypesFileTypeMap总是为PNG文件返回“application / octet-stream”? - Why does MimetypesFileTypeMap always return “application/octet-stream” for PNG file? Spring文件上传内容类型验证,总是八位字节流? - Spring file upload content type validation, always octet-stream? 生成 application/octet-stream 和 application/json - Producing application/octet-stream and application/json 将String转换为application / octet-stream Java - Convert String to application/octet-stream Java 多部分/混合和应用程序/八位位组流 - multipart/mixed and application/octet-stream 为什么除非我将文件命名为 .html,否则上传到 S3 的文件的内容类型为 application/octet-stream? - Why does file uploaded to S3 have content type application/octet-stream unless I name the file .html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM