我想在使用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)
如何使用流将返回值更改为某些内容,例如流输出?或者,我也可以使用javax.ws.rs.core.Response
。
当我在生成器中设置returResponse
参数时,此方法将返回Response
,但从该OpenAPI规范生成的所有其他方法也将返回,我需要对齐所有实现和测试。
我已经考虑过定制模板,就像这个答案中建议的那样,但是对于这么小的改变来说,这感觉太多了。
您可以通过使用类型映射导入映射来存档类似结果
<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-Generer-maven-plugin使用它时只需要采取几个步骤
>
创建一个新的 maven 模块。
为扩展 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);
}
}
这个代码生成器由< code > jaxrs-spec-streaming output 标识
添加 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}
替换为实际的软件包名称)
在生成的模块中,只保留生成器类和< code>META-INF/services中的< code>CodegenConfig。按照上面步骤2中的描述修改生成器类。
然后可以通过< code > open API-generator-maven-plugin 安装(< code>mvn install)和使用maven模块。设置< code>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>
confiOptions
可以用作jaxrs-spec
生成器。