从OpenAPI规范使用jaxrs-spec生成器生成的接口返回StreamingOutput &g



我想在使用jaxrs-spec实现从OpenAPI规范生成的接口时返回StreamingOutput。默认实现只使用java.io.File作为返回值。

我们从数据库中加载用户生成的数据,甚至从不同的服务中加载数据流,并且希望避免仅仅为了符合接口而创建临时文件。

使用maven插件的当前配置如下所示

<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.2.0</version>
<executions>
<execution>
<!-- <phase/> and <goals/> -->
<configuration>
<inputSpec>path/to/openapi/spec.yml</inputSpec>
<generatorName>jaxrs-spec</generatorName>
<configOptions>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
<interfaceOnly>true</interfaceOnly>
<useSwaggerAnnotations>false</useSwaggerAnnotations>
<sourceFolder>src/main/java</sourceFolder>
<dateLibrary>java8</dateLibrary>
<title>${title}</title>
<apiPackage>com.example.api</apiPackage>
<modelPackage>com.example.model</modelPackage>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
在此设置中,我使用以下OpenAPI定义
/documents/{id}:
get:
summary: Get the given document as PDF
operationId: getDocument
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
200:
description: Get the document
content:
application/pdf:
schema:
type: string
format: binary
302:
description: Redirect to the location of the PDF document
headers:
Location:
description: The URL from where to get the PDF document
schema:
type: string
404:
description: Document not found

导致这个方法

@GET
@Path("/{id}")
@Produces({ "application/pdf" })
File getDocument(@PathParam("id") String id)

我怎么能改变返回值的东西使用流,例如一个StreamingOutput?或者,我也可以使用javax.ws.rs.core.Response

当我在生成器中设置returnResponse参数时,此方法将返回Response,但从此OpenAPI规范生成的所有其他方法也是如此,我需要对齐所有实现和测试。

我已经研究过自定义模板,正如这个答案所建议的那样,但是对于这样一个小小的改变来说,这感觉太大了。

您可以通过使用TypeMapping+ImportMapping

来存档类似的结果
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.2.1</version>
<configuration>
.. rest of configuration ..
<typeMappings>
<typeMapping>binary=OutputStream</typeMapping>
<typeMapping>file=OutputStream</typeMapping>
</typeMappings>
<importMappings>
<importMapping>OutputStream=my.custom.OutputStream</importMapping>
</importMappings>
</configuration>
<executions>
.. your executions ..       
</executions>
</plugin>

按照Rob的建议,当通过Maven和openapi-generator-maven-plugin使用这个工具时,只需要执行几个步骤

  1. 创建一个新的maven模块

  2. 为代码生成器创建一个扩展JavaJAXRSSpecServerCodegen的类。这增加了"StreamingOutput"作为"file""binary"的类型映射(只有"file"对我的规范是足够的),还增加了javax.ws.rs.core.StreamingOutput的导入

    public class JaxrsSpecStreamingOutputGenerator extends JavaJAXRSSpecServerCodegen {
    protected static final String GENERATOR_NAME = "jaxrs-spec-streamingoutput";
    @Override
    public String getName() {
    return GENERATOR_NAME;
    }
    @Override
    public String getHelp() {
    return super.getHelp() + "njava.io.File is replaced with " + StreamingOutput.class.getName();
    }
    public JaxrsSpecStreamingOutputGenerator() {
    String shortName = "StreamingOutput";
    importMapping.put(shortName, StreamingOutput.class.getName());
    // use 'StreamingOutput' for 'binary' and 'file'
    typeMapping.put("binary", shortName);
    typeMapping.put("file", shortName);
    }
    }
    

    此代码生成器由jaxrs-spec-streamingoutput

    标识
  3. 添加META-INF/services/org.openapitools.codegen.CodegenConfig,内容如下:

    ${package}.JaxrsSpecStreamingOutputGenerator
    

    (将${package}替换为实际的包名)


或者,maven模块和使用OpenAPI Generator CLI元命令生成的所有必要文件。

java -jar openapi-generator-cli-6.2.1.jar 
meta 
-o jaxrs-spec-streamingoutput 
-n jaxrs-spec-streamingoutput 
-p ${package}

(将${package}替换为实际的包名)

在生成的模块中,只保留生成器类和META-INF/services中的CodegenConfig。按照上面步骤2中所描述的调整生成器类。


maven模块可以安装(mvn install)并通过openapi-generator-maven-plugin使用。设置generatorName并包含maven模块作为插件依赖项:

<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${plugin-version}</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>...</goals>
<configuration>
<inputSpec>...</inputSpec>
<!-- 1. set generator name -->
<generatorName>jaxrs-spec-streamingoutput</generatorName>
<configOptions>...</configOptions>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<!-- 
2. add this module to the dependencies
e.g. '${project.groupId}:api-codegen:${version}'
-->
</dependency>
</dependencies>
</plugin>

configOptions可以用作jaxrs-spec生成器。

最新更新