Java7环境openapi-generator生成ApiClient编译报错,求适配配置
修复步骤
1. 补全Java7兼容的RESTEasy依赖
你选了resteasy作为生成客户端的底层库,缺失对应依赖导致javax.ws.rs.client包找不到,在pom.xml的<dependencies>节点添加如下适配Java7的依赖(3.x版本的RESTEasy兼容Java7,4.x及以上仅支持Java8+):
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>3.15.6.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson2-provider</artifactId> <version>3.15.6.Final</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.1.1</version> </dependency>
2. 修复AnyOf类找不到问题
这个问题来自三个原因,逐一调整:
- 首先删除本地
com.company.project.jbpm.rest目录下所有之前生成的旧代码,避免旧文件引用错误的包路径(你报错里搜索的包是com.project.rest.model,和你配置的com.company.project.jbpm.rest.model不一致,就是旧代码残留导致) - 在插件的
<configOptions>节点新增两个配置,关闭Java7不支持的Nullable注解和anyOf默认生成逻辑:
<openApiNullable>false</openApiNullable> <oneOfAnyOfImplementation>interface</oneOfAnyOfImplementation>
- 如果你的yaml文件里定义了
oneOf/anyOf结构,Java7环境下建议简化yaml去掉这类多态定义,避免生成不兼容的类。
3. 补全maven编译版本配置
确保pom.xml里配置了maven-compiler-plugin指定编译级别为Java7:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin>
调整后的openapi插件完整配置参考
<plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>4.3.1</version> <executions> <execution> <id>regular</id> <goals> <goal>generate</goal> </goals> <configuration> <inputSpec>${project.basedir}/src/main/resources/META-INF/xyz.yaml</inputSpec> <generatorName>java</generatorName> <output>${project.basedir}</output> <generateApiTests>true</generateApiTests> <generateModelTests>false</generateModelTests> <withXml>true</withXml> <generateSupportingFiles>false</generateSupportingFiles> <skipValidateSpec>true</skipValidateSpec> <environmentVariables> <supportingFiles>ApiClient.java,ApiResponse.java,ApiException.java,Configuration.java,JavaTimeFormatter.java,RFC3339DateFormat.java,JSON.java,Pair.java,ServerConfiguration.java,ServerVariable.java,StringUtil.java,Authentication.java,HttpBasicAuth.java,ApiKeyAuth.java,HttpBearerAuth.java</supportingFiles> </environmentVariables> <configOptions> <apiPackage>com.company.project.jbpm.rest.api</apiPackage> <modelPackage>com.company.project.jbpm.rest.model</modelPackage> <dateLibrary>java7</dateLibrary> <library>resteasy</library> <useTags>true</useTags> <!-- 新增Java7兼容配置 --> <openApiNullable>false</openApiNullable> <oneOfAnyOfImplementation>interface</oneOfAnyOfImplementation> </configOptions> </configuration> </execution> </executions> </plugin>
配置调整完成后执行mvn clean compile重新生成代码并编译即可。
内容的提问来源于stack exchange,提问作者Sap
相关产品推荐
相关产品推荐

